Presentation is loading. Please wait.

Presentation is loading. Please wait.

The make Utility Programming Tools and Environments Winter 2006.

Similar presentations


Presentation on theme: "The make Utility Programming Tools and Environments Winter 2006."— Presentation transcript:

1 The make Utility Programming Tools and Environments Winter 2006

2 make The make utility automates certain tasks (usually simple command-line stuff) compiling multi-file programs archiving/extracting installation Often used to manage builds runs the compiler only when necessary uses file modify times to decide when it is necessary

3 Makefiles A basic makefile contains targets A target consists of: Target name – file to be built List of dependencies (if any) Command to build the target

4 Rules target : dependencies command1 command2 … Each command executed in its own subshell Each command must start w/a tab These MUST be tabs!!!

5 Dependency List A list of files that the target depends upon Target is only built if it doesn't not exist, or if it exists, but is older than all of the dependencies. A dependency may also appear as a target in the same makefile; these are then intermediate targets This relationship is checked recursively, down the dependency tree

6 Simple Example linecount : foo.c wc –l foo.c > linecount This rule would tell make that the file linecount depends on the file foo.c To build linecount the command “ wc –l foo.c > linecount ” should be run If linecount doesn't exist, or foo.c is newer (modified more recently than linecount ), the wc command will be executed

7 (cont) $ make target main check dependencies, main.o & subs.o 1. main.o check its dependenies, main.cc & subs.h 2. subs.o etc. Does main exist? If so, is it newer than its dependencies? If no, execute command-line

8 Input Files Specify a makefile using the -f option to make make -f myMakefile If not specified, make looks in the current directory for (in order): 1. makefile 2. Makefile

9 Simple Makefile for building a program. main: main.o subs.o gcc –o main main.o subs.o subs.o: subs.c subs.h gcc –c subs.c main.o: main.c subs.h gcc –c main.c TABS!

10 Make command line (simplified) make [options] [targets] options include “-f filename” use filename as the Makefile “-n” don’t do anything – just print what needs to be done. targets are what should be built (if needed).

11 Specifying a target Just specify the target after the make command: make subs.o If no target is specified, the first target listed in the file is created

12 Revisit Example A target doesn't need dependencies, nor does its command-line need to make create the target: clean : - \rm *.o - \rm main $ make clean will cause those files to be removed (assuming there is no file called clean in the directory. See next slide.)

13 Phony targets (GNU only) A target can be specified as phony, so that it is always executed:.PHONY : clean clean : - \rm *.o - \rm main The dash in front of the commands tells make to ignore the return values; to continue executing, even if an error is returned.

14 Macros You can create macros (make variables) OBJS = subs.o main.o main: $(OBJS) gcc –o main $(OBJS)

15 Command Modifiers If a command starts with @, it won't be echoed to stdout @echo “Hello there A preceding - will tell make to ignore return status clean: -\rm foo -\rm bar

16 Fancy Stuff – Macro Substitution Evaluates to the value of a macro after some substitutions: SOURCE = main.c subs.c OBJS = ${SOURCE:.c=.o} now OBJS is main.o subs.o

17 Automatic Variables $@Name of current target $<Name of first prerequisite $^ Name of all prerequisites, with spaces in between $?Name of all prerequisites newer than the target

18 Suffix Rules General rules for building files that end with some suffix from files with the same name but a different suffix For example, how to build a.o file from a.c file %.o : %.c $(cc) $< -o $@

19 Example Suffix Rule.c.o: gcc –c $< This rule tells Make how to create a.o file any time is has a.c file (and needs the.o file to build a target).

20 A Complete Example.c.o: gcc –c $< SOURCE = main.c subs.c OBJS = ${SOURCE:.c=.o} main: $(OBJS) gcc –o main $(OBJS)

21 Comments and other Makefile notes Comments begin with a ‘#’ Can be placed at the beginning of a line or after a non-comment line Lines that are too long can be continued on the next line by placing a ‘\’ at the end of the first line

22 Basic Makefile example program : main.o iodat.o dorun.o gcc -o program main.o iodat.o dorun.o main.o : main.c gcc -c main.c iodat.o : iodat.c gcc -c iodat.c dorun.o : dorun.c gcc -c dorun.c

23 Simplifying the example Makefile with macros OBJS = main.o iodat.o dorun.o CC = /usr/bin/gcc program : ${OBJS} ${CC} -o $@ ${OBJS} main.o : main.c ${CC} -c $? iodat.o : iodat.c ${CC} -c $? dorun.o : dorun.c ${CC} -c $?

24 Simplifying the example Makefile again OBJS = main.o iodat.o dorun.o CC = /usr/bin/gcc program : ${OBJS} ${CC} -o $@ ${OBJS}

25 Other useful Makefile tips Include a way to clean up your mess clean: /bin/rm -f *.o core Include a target to build multiple programs all: program1 program2 program3

26 Epilogue We've looked at rather basic makefiles, but you already have a useful working knowledge. If you need it, make is capable of a good bit more. Read gnu's site. http://www.gnu.org/software/make/ma nual/make.html


Download ppt "The make Utility Programming Tools and Environments Winter 2006."

Similar presentations


Ads by Google