20
makefiles makefiles

Makefiles. makefiles Problem: You are working on one part of a large programming project (e. g., MS Word). It consists of hundreds of individual.c files

Embed Size (px)

Citation preview

Page 1: Makefiles. makefiles Problem: You are working on one part of a large programming project (e. g., MS Word).  It consists of hundreds of individual.c files

makefilesmakefiles

Page 2: Makefiles. makefiles Problem: You are working on one part of a large programming project (e. g., MS Word).  It consists of hundreds of individual.c files

makefilesmakefiles

Problem: You are working on one part of a Problem: You are working on one part of a large programming project (e. g., MS large programming project (e. g., MS Word).Word).

It consists of hundreds of individual .c files It consists of hundreds of individual .c files all linked together to form winword.exe.all linked together to form winword.exe.

You make a change to one of these .c files You make a change to one of these .c files and now need to rebuild word.exe.and now need to rebuild word.exe.

How do you do it without having to How do you do it without having to recompile hundreds of .c files that haven’t recompile hundreds of .c files that haven’t changed?changed?

Page 3: Makefiles. makefiles Problem: You are working on one part of a large programming project (e. g., MS Word).  It consists of hundreds of individual.c files

w/out makefilesw/out makefiles

g++ -o word.exe f1.c f2.c f3.c f4.c … f999.cg++ -o word.exe f1.c f2.c f3.c f4.c … f999.cBut only f4.c has changed and only needs to be But only f4.c has changed and only needs to be recompiled!recompiled!

Compiler option to create intermediate (object Compiler option to create intermediate (object modules) files:modules) files:g++ -c f1.cg++ -c f1.cg++ -c f2.cg++ -c f2.c……g++ -c f999.cg++ -c f999.cg++ -o word.exe f1.o f2.o f3.o … f999.og++ -o word.exe f1.o f2.o f3.o … f999.o

Then I just manually do: g++ -c f4.c and then relink.Then I just manually do: g++ -c f4.c and then relink.

Page 4: Makefiles. makefiles Problem: You are working on one part of a large programming project (e. g., MS Word).  It consists of hundreds of individual.c files

But how can I automatically determine But how can I automatically determine only what needs to be recompiled?only what needs to be recompiled?

Write a program that looks at the modification Write a program that looks at the modification time of the .c file and the .o (or .exe) file. If the time of the .c file and the .o (or .exe) file. If the mod time of the .c file is more recent than the mod time of the .c file is more recent than the mod time of the .o (or .exe) file, then we know mod time of the .o (or .exe) file, then we know that we know that we need to recompile and that we know that we need to recompile and relink.relink.

But I don’t want to write this program! (Someone But I don’t want to write this program! (Someone already built this into the Java compiler, and MS already built this into the Java compiler, and MS Visual Studio.)Visual Studio.)

Page 5: Makefiles. makefiles Problem: You are working on one part of a large programming project (e. g., MS Word).  It consists of hundreds of individual.c files

makefilesmakefiles

Consist of:Consist of:

1.1. Comments (begin with #)Comments (begin with #)

2.2. DefinitionsDefinitions

3.3. DependenciesDependencies

4.4. CommandsCommands

Page 6: Makefiles. makefiles Problem: You are working on one part of a large programming project (e. g., MS Word).  It consists of hundreds of individual.c files

makefilesmakefiles

Consist of:Consist of:1.1. Comments (begin with #)Comments (begin with #)2.2. DefinitionsDefinitions3.3. DependenciesDependencies4.4. CommandsCommands

Comments:Comments:

### this is a comment.# this is a comment.##

Page 7: Makefiles. makefiles Problem: You are working on one part of a large programming project (e. g., MS Word).  It consists of hundreds of individual.c files

makefilesmakefiles

Consist of:Consist of:1.1. Comments (begin with #)Comments (begin with #)2.2. DefinitionsDefinitions3.3. DependenciesDependencies4.4. CommandsCommands

Definitions (are like constants):Definitions (are like constants):CC = g++ -gCC = g++ -gororCC = g++ -O3CC = g++ -O3……

to use these definitions, evaluate with $(CC)to use these definitions, evaluate with $(CC)

Page 8: Makefiles. makefiles Problem: You are working on one part of a large programming project (e. g., MS Word).  It consists of hundreds of individual.c files

3. Dependencies3. Dependencies

a:<tab>b c d …a:<tab>b c d … This means that This means that aa is dependent upon (needs) is dependent upon (needs) bb and and

cc and and dd … …

What is word.exe dependent upon (below)?What is word.exe dependent upon (below)? g++ -o word.exe f1.o f2.o f3.o … f999.og++ -o word.exe f1.o f2.o f3.o … f999.o word.exe is dependent upon f1.o, f2.o, …, f999.oword.exe is dependent upon f1.o, f2.o, …, f999.o

word.exe:<tab>f1.o f2.o … f999.oword.exe:<tab>f1.o f2.o … f999.o

What is f1.o dependent upon?What is f1.o dependent upon?

Page 9: Makefiles. makefiles Problem: You are working on one part of a large programming project (e. g., MS Word).  It consists of hundreds of individual.c files

Dependencies & 4. CommandsDependencies & 4. Commands

UsuallyUsually, a dependency is followed by a , a dependency is followed by a command to rebuild/recreate/update the entity to command to rebuild/recreate/update the entity to the left of the colon (called a tag).the left of the colon (called a tag).

Dependencies are hierarchical (i.e., top-down).Dependencies are hierarchical (i.e., top-down).word.exe:<tab>word.exe:<tab> f1.o f2.o … f999.of1.o f2.o … f999.o<tabs><tabs> g++ -o word.exe f1.o f2.o … f999.og++ -o word.exe f1.o f2.o … f999.o

f1.o:<tab>f1.o:<tab> f1.cf1.c<tabs><tabs> g++ -c f1.cg++ -c f1.c

f2.o:<tab>f2.o:<tab> f2.cf2.c<tabs><tabs> g++ -c f2.cg++ -c f2.c……

Page 10: Makefiles. makefiles Problem: You are working on one part of a large programming project (e. g., MS Word).  It consists of hundreds of individual.c files

makefile syntaxmakefile syntax

target1 target2 target3 : prerequisite1 prerequisite2target1 target2 target3 : prerequisite1 prerequisite2

<tab>command1<tab>command1

<tab>command2<tab>command2

<tab>command3<tab>command3

1.1.One or more targets (to the left of the colon).One or more targets (to the left of the colon).

2.2.Zero or more prereqs (after the colon).Zero or more prereqs (after the colon).

3.3.Zero or more commands (each Zero or more commands (each mustmust be preceeded by a be preceeded by a tab).tab).

Page 11: Makefiles. makefiles Problem: You are working on one part of a large programming project (e. g., MS Word).  It consists of hundreds of individual.c files

““Standard phony” targetsStandard phony” targets

allall Perform all tasks to build the applicationPerform all tasks to build the application

installinstallCreate an installation of the application from the Create an installation of the application from the compiled binariescompiled binaries

cleanclean Delete the binary files generated from sourcesDelete the binary files generated from sources

distcleandistclean Delete all the generated files that were not in Delete all the generated files that were not in the original source distributionthe original source distribution

TAGSTAGS Create a tags table for use by editorsCreate a tags table for use by editors

infoinfo Create GNU info files from their Texinfo sourcesCreate GNU info files from their Texinfo sources

checkcheck Run any tests associated with this applicationRun any tests associated with this application

docsdocs Create (doxygen) documentation. (gjg)Create (doxygen) documentation. (gjg)

Page 12: Makefiles. makefiles Problem: You are working on one part of a large programming project (e. g., MS Word).  It consists of hundreds of individual.c files

Example (including definitions and Example (including definitions and comments)comments)

#for debug version:#for debug version:CC = g++ -gCC = g++ -g

#for production version:#for production version:#CC = g++ -O3#CC = g++ -O3

word.exe:<tab>word.exe:<tab> f1.o f2.o … f999.of1.o f2.o … f999.o<tabs><tabs> $(CC) -o word.exe f1.o f2.o … f999.o$(CC) -o word.exe f1.o f2.o … f999.o

f1.o:<tab>f1.o:<tab> f1.cf1.c<tabs><tabs> $(CC) -c f1.c$(CC) -c f1.c……

Page 13: Makefiles. makefiles Problem: You are working on one part of a large programming project (e. g., MS Word).  It consists of hundreds of individual.c files

Example (more than 1 command)Example (more than 1 command)

#for debug version:#for debug version:

CC = g++ -gCC = g++ -g

word.exe:word.exe: f1.o f2.o … f999.of1.o f2.o … f999.o

echo link word.exeecho link word.exe

$(CC) -o word.exe f1.o f2.o … f999.o$(CC) -o word.exe f1.o f2.o … f999.o

f1.o:f1.o: f1.cf1.c

echo compile f1.cecho compile f1.c

$(CC) -c f1.c$(CC) -c f1.c

……

Page 14: Makefiles. makefiles Problem: You are working on one part of a large programming project (e. g., MS Word).  It consists of hundreds of individual.c files

Example (make more than one)Example (make more than one)

#for debug version:#for debug version:CC = g++ -gCC = g++ -g

all:all: word.exe fred.exe …word.exe fred.exe …

word.exe:word.exe: f1.o f2.o … f999.of1.o f2.o … f999.oecho link word.exeecho link word.exe

$(CC) -o word.exe f1.o f2.o … f999.o$(CC) -o word.exe f1.o f2.o … f999.o

f1.o:f1.o: f1.cf1.cecho compile f1.cecho compile f1.c$(CC) -c f1.c$(CC) -c f1.c

……

Page 15: Makefiles. makefiles Problem: You are working on one part of a large programming project (e. g., MS Word).  It consists of hundreds of individual.c files

Example make commandsExample make commands

makemake Checks the first dependency that appears in the file Checks the first dependency that appears in the file

makefilemakefile.. make allmake all

Checks the Checks the allall dependency in dependency in makefilemakefile.. make word.exemake word.exe make f1.omake f1.o make fred.exemake fred.exe make –f myMakeFile allmake –f myMakeFile all

Checks the Checks the allall dependency in file dependency in file myMakeFilemyMakeFile..

Page 16: Makefiles. makefiles Problem: You are working on one part of a large programming project (e. g., MS Word).  It consists of hundreds of individual.c files

AssignmentAssignment

1.1. Create a file called f1.cpp. It should contain Create a file called f1.cpp. It should contain the following function:the following function:

int squareIt ( int x ) {int squareIt ( int x ) {//insert code to calc and return the square of x//insert code to calc and return the square of x

}}

2.2. Create a file called f2.cpp. It should contain Create a file called f2.cpp. It should contain the following function:the following function:

int cubeIt ( int x ) {int cubeIt ( int x ) {//insert code to calc and return the cube of x//insert code to calc and return the cube of x

}}

Page 17: Makefiles. makefiles Problem: You are working on one part of a large programming project (e. g., MS Word).  It consists of hundreds of individual.c files

Assignment (cont’d.)Assignment (cont’d.)

3. Create a main program in a file called main.cpp3. Create a main program in a file called main.cpp

#include <stdio.h>#include <stdio.h>

extern int squareIt ( int x );extern int squareIt ( int x );extern int cubeIt ( int x );extern int cubeIt ( int x );

int main ( int argc, char* argv[] ) {int main ( int argc, char* argv[] ) { … …}}

Page 18: Makefiles. makefiles Problem: You are working on one part of a large programming project (e. g., MS Word).  It consists of hundreds of individual.c files

Assignment (cont’d.)Assignment (cont’d.)

Main should ask the user to enter a Main should ask the user to enter a number. Main should then call the two number. Main should then call the two functions, add up the values, and then functions, add up the values, and then print out this sum.print out this sum.

Main should repeat the above 3 times.Main should repeat the above 3 times.

Get this to compile, link, and run.Get this to compile, link, and run.

Page 19: Makefiles. makefiles Problem: You are working on one part of a large programming project (e. g., MS Word).  It consists of hundreds of individual.c files

Assignment (cont’d.)Assignment (cont’d.)

Then create a makefile which should only Then create a makefile which should only recompile and relink only when changes recompile and relink only when changes are made.are made.

The makefile should also contain a tag The makefile should also contain a tag called called cleanclean. Whenever the user types . Whenever the user types make cleanmake clean, this tag should simply delete , this tag should simply delete all .o and .exe files. See the man page for all .o and .exe files. See the man page for rm. If no .o or .exe files are present, rm. If no .o or .exe files are present, make make cleanclean should not report any errors. should not report any errors.

Page 20: Makefiles. makefiles Problem: You are working on one part of a large programming project (e. g., MS Word).  It consists of hundreds of individual.c files

Assignment (cont’d.)Assignment (cont’d.)

Fully document each C/C++ file and each Fully document each C/C++ file and each function with doxygen.function with doxygen.