brandon packard. why make? so far, you have probably worked on relatively small projects coding...

20
Brandon Packard

Upload: thomas-carroll

Post on 19-Jan-2018

214 views

Category:

Documents


0 download

DESCRIPTION

Are makefiles worth it? Imagine typing out javac file1.java, file2.java, file3.java, …., file1600.java Could store the compilation command in a text file, but there is an easier way

TRANSCRIPT

Page 1: Brandon Packard. Why make? So far, you have probably worked on relatively small projects Coding projects…

Brandon Packard

Page 2: Brandon Packard. Why make? So far, you have probably worked on relatively small projects Coding projects…

Why make?So far, you have probably worked on

relatively small projects

Coding projects can become huge

My research consists of 1600 java files (not all written by me of course)

Page 3: Brandon Packard. Why make? So far, you have probably worked on relatively small projects Coding projects…

Are makefiles worth it?Imagine typing out javac file1.java, file2.java,

file3.java, …., file1600.java

Could store the compilation command in a text file, but there is an easier way

Page 4: Brandon Packard. Why make? So far, you have probably worked on relatively small projects Coding projects…

What is make?Make is a tool that lets you compile code with

a single small command.

Implemented on tux, with the “make” command.

Requires a Makefile to work

Page 5: Brandon Packard. Why make? So far, you have probably worked on relatively small projects Coding projects…

Writing a Makefile4 major pieces (for our purposes):

VariablesTargetsCommandsPrerequisites

Page 6: Brandon Packard. Why make? So far, you have probably worked on relatively small projects Coding projects…

VariablesDeclared at the top of the file

Much like variables in bash

CC = g++

Now you can use $(CC) instead of g++

Page 7: Brandon Packard. Why make? So far, you have probably worked on relatively small projects Coding projects…

VariablesVariables can be set to represent an entire

group of files

Can make the main body of the make file much easier to read.

Page 8: Brandon Packard. Why make? So far, you have probably worked on relatively small projects Coding projects…

TargetsThese are what you reference from the

command line

For makefiles that can do multiple things, this lets you specify which behaviour you want

Syntax is the name of the target followed by a colon.

Page 9: Brandon Packard. Why make? So far, you have probably worked on relatively small projects Coding projects…

Target examplesclean:

run:

To run the clean target, you would type “make clean” at the command line.

Default target is the first one in the file.

Page 10: Brandon Packard. Why make? So far, you have probably worked on relatively small projects Coding projects…

CommandsThese are what get executed when you run

make.

Go on the line under the target

Can compile code, run files, clean up directories, mostly anything you can do in the shell

Page 11: Brandon Packard. Why make? So far, you have probably worked on relatively small projects Coding projects…

Example Commands$(CC) -c markov.cpp

Compiles the code but does not link it

rm -f *.oRemoves all object files from the current

directory

$(someProgram) > $(someFile)Runs the program in the first variable and puts

the output to the file held as the second variable

Page 12: Brandon Packard. Why make? So far, you have probably worked on relatively small projects Coding projects…

Prerequisites Used to the commands have the files they

need

Can also be used to order targets

If the prerequisite is empty, another target may be ran to obtain it

Page 13: Brandon Packard. Why make? So far, you have probably worked on relatively small projects Coding projects…

PrerequisitesAre placed on the same line as the target,

after the colon:run: someJavaFile.class

If the class file is not present, make will try to obtain it by running other targets (such as a compile target)

Page 14: Brandon Packard. Why make? So far, you have probably worked on relatively small projects Coding projects…

A (fairly) simple make fileRESULT=helloall: $(RESULT) $(RESULT): main.o g++ main.o -o $(RESULT)main.o: main.cpp g++ -c main.cpp clean: rm -rf *o $(RESULT)

Page 15: Brandon Packard. Why make? So far, you have probably worked on relatively small projects Coding projects…

A (fairly) simple make file

RESULT=helloall: $(RESULT) $(RESULT): main.o g++ main.o -o $(RESULT)main.o: main.cpp g++ -c main.cpp clean: rm -rf *o $(RESULT)

Variable

Target

Prerequisite

Command

Page 16: Brandon Packard. Why make? So far, you have probably worked on relatively small projects Coding projects…

A (fairly) simple make file

RESULT=helloall: $(RESULT) $(RESULT): main.o g++ main.o -o $(RESULT)main.o: main.cpp g++ -c main.cpp clean: rm -rf *o $(RESULT)

Command:make all(assume we only have main.cpp)

1. all target calls the RESULT target

2. RESULT needs main.o, butdoes not have it. Calls main.o target

Page 17: Brandon Packard. Why make? So far, you have probably worked on relatively small projects Coding projects…

A (fairly) simple make fileRESULT=helloall: $(RESULT) $(RESULT): main.o g++ main.o -o $(RESULT)main.o: main.cpp g++ -c main.cpp clean: rm -rf *o $(RESULT)

3. main.o target runs this command to compile (but not link) main.cpp

4. RESULT now has its prerequisites fulfilled - links object file into executable

Command:make all(assume we only have main.cpp)

Page 18: Brandon Packard. Why make? So far, you have probably worked on relatively small projects Coding projects…

A (fairly) simple make fileRESULT=helloall: $(RESULT) $(RESULT): main.o g++ main.o -o $(RESULT)main.o: main.cpp g++ -c main.cpp clean: rm -rf *o $(RESULT)

command: make cleanthe clean target will remove all of the object files in the directory, as well as the executable

Page 19: Brandon Packard. Why make? So far, you have probably worked on relatively small projects Coding projects…

Makefiles can be very complexMakefiles can be full of special targets,

implicit rules, and even conditionals

Simple makefiles should suit our purposes well enough

Will be using make files to submit your other programming assignments

Page 20: Brandon Packard. Why make? So far, you have probably worked on relatively small projects Coding projects…

FinQuestions?

Comments?

Concerns?

Doubts about my sanity?