Presentation is loading. Please wait.

Presentation is loading. Please wait.

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 all linked.

Similar presentations


Presentation on theme: "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 all linked."— Presentation transcript:

1 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 all linked together to form word.exe.  You make a change to one of these.c files and now need to rebuild word.exe.  How do you do it without having to recompile hundreds of.c files that haven’t changed?

2 w/out makefiles g++ -o word.exe f1.c f2.c f3.c f4.c … f999.c But only f4.c has changed and only needs to be recompiled! Compiler option to create intermediate (object modules) files: g++ -c f1.c g++ -c f2.c … g++ -c f999.c g++ -o word.exe f1.o f2.o f3.o … f999.o But how can I automatically determine only what needs to be recompiled?

3 makefiles  Consist of:  Comments (begin with #)  Definitions  Dependencies  Commands  Definitions (like constants): CC = g++ -g Or CC = g++ -O3 … To use these definitions we evaluate $(CC)

4 Dependency a: b c d …  Means that a is dependent upon (needs) b and c and d …  What is word.exe dependent upon?  g++ -o word.exe f1.o f2.o f3.o … f999.o  word.exe is dependent upon f1.o, f2.o, …, f999.o  word.exe: f1.o f2.o … f999.o  What is f1.o dependent upon?

5 Dependency & command  Usually, a dependency is followed by a command to rebuild/recreate/update the entity to the left of the colon (called a tag).  Dependencies are hierarchical. word.exe:f1.o f2.o … f999.o g++ -o word.exe f1.o f2.o … f999.o f1.o:f1.c g++ -c f1.c f2.o:f2.c g++ -c f2.c …

6 Example (including definitions and comments) #for debug version: CC = g++ -g #for production version: #CC = g++ -O3 word.exe:f1.o f2.o … f999.o $(CC) -o word.exe f1.o f2.o … f999.o f1.o:f1.c $(CC) -c f1.c …

7 Example (more than 1 command) #for debug version: CC = g++ -g word.exe:f1.o f2.o … f999.o echo link word.exe $(CC) -o word.exe f1.o f2.o … f999.o f1.o:f1.c echo compile f1.c $(CC) -c f1.c …

8 Example (make more than one) #for debug version: CC = g++ -g all:word.exe fred.exe … word.exe:f1.o f2.o … f999.o echo link word.exe $(CC) -o word.exe f1.o f2.o … f999.o f1.o:f1.c echo compile f1.c $(CC) -c f1.c …

9  make  Checks the first dependency that appears  make all  make word.exe  make f1.o  make fred.exe

10 Lab Assignment 1. Create a file called f1.cpp. It should contain the following function: int squareIt ( int x ) { //insert code to calc and return the square of x } 2. Create a file called f2.cpp. It should contain the following function: int cubeIt ( int x ) { //insert code to calc and return the cube of x }

11 Lab Assignment 3. Create a main program in a file called main.cpp #include #include extern int squareIt ( int x ); extern int cubeIt ( int x ); int main ( int argc, char* argv[] ) { …}

12 Lab Assignment  Main should ask the user to enter a number. Main should then call the two functions, add up the values, and then print out this sum.  Main should repeat the above 3 times.  Get this to compile, link, and run.

13 Lab Assignment  Then create a makefile which should only recompile and relink only when changes are made.  The makefile should also contain a tag called clean. Whenever the user types make clean, this tag should simply delete all.o and.exe files. See the man page for rm. If no.o or.exe files are present, make clean should not report any errors.


Download ppt "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 all linked."

Similar presentations


Ads by Google