26
The make Utility Programming Tools and Environments Winter 2006

The make Utility

  • Upload
    nola

  • View
    18

  • Download
    0

Embed Size (px)

DESCRIPTION

The make Utility. Programming Tools and Environments Winter 2006. 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 - PowerPoint PPT Presentation

Citation preview

Page 1: The make Utility

The make Utility

Programming Tools and EnvironmentsWinter 2006

Page 2: The make Utility

makeThe 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

Page 3: The make Utility

Makefiles

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

Page 4: The make Utility

Rules

target : dependenciescommand1command2…

Each command executed in its own subshell

Each command must start w/a tab

These M

UST be tabs!!

!

Page 5: The make Utility

Dependency ListA 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 any 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

Page 6: The make Utility

Simple Examplelinecount : 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

Page 7: The make Utility

(cont)

$ maketarget main

check dependencies, main.o & subs.o1. main.o

check its dependenies, main.cc & subs.h

2. subs.oetc.

Does main exist? If so, is it newer than its dependencies?

If no, execute command-line

Page 8: The make Utility

Input FilesSpecify 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

Page 9: The make Utility

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!

Page 10: The make Utility

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).

Page 11: The make Utility

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

Page 12: The make Utility

Revisit ExampleA 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.)

Page 13: The make Utility

Phony targets (GNU only)

A target can be specified as phony, so that it is always executed:.PHONY : cleanclean :

- \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.

Page 14: The make Utility

MacrosYou can create macros (make variables)

OBJS = subs.o main.o

main: $(OBJS)

gcc –o main $(OBJS)

Page 15: The make Utility

Command ModifiersIf a command starts with @, it won't be echoed to stdout@echo “Hello there

A preceding - will tell make to ignore return statusclean:

-\rm foo

-\rm bar

Page 16: The make Utility

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

Page 17: The make Utility

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

Page 18: The make Utility

Suffix Rules

General rules for building files that end with some suffix from files with the same name but a different suffixFor example, how to build a .o file from a .c file%.o : %.c

$(cc) $< -o $@

Page 19: The make Utility

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).

Page 20: The make Utility

A Complete Example

.c.o:

gcc –c $<

SOURCE = main.c subs.c

OBJS = ${SOURCE:.c=.o}

main: $(OBJS)gcc –o main $(OBJS)

Page 21: The make Utility

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

Page 22: The make Utility

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

Page 23: The make Utility

Simplifying the example Makefile with macros

OBJS = main.o iodat.o dorun.oCC = /usr/bin/gccprogram : ${OBJS}${CC} -o $@ ${OBJS}

main.o : main.c${CC} -c $?

iodat.o : iodat.c ${CC} -c $?

dorun.o : dorun.c ${CC} -c $?

Page 24: The make Utility

Simplifying the example Makefile again

OBJS = main.o iodat.o dorun.o

CC = /usr/bin/gcc

program : ${OBJS}

${CC} -o $@ ${OBJS}

Page 25: The make Utility

Other useful Makefile tips

Include a way to clean up your messclean:

/bin/rm -f *.o core

Include a target to build multiple programs

all: program1 program2 program3

Page 26: The make Utility

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/manual/make.html