Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Makefiles. makefiles Problem: You are working on one part of a large programming project (e. g., MS Word).  It consists of hundreds of individual.cpp."— Presentation transcript:

1 makefiles

2 makefiles Problem: You are working on one part of a large programming project (e. g., MS Word).  It consists of hundreds of individual.cpp files all linked together to form winword.exe.  You make a change to one of these.cpp files and now need to rebuild word.exe.  How do you do it without having to recompile hundreds of.cpp files that haven’t changed?

3 w/out makefiles g++ -o word.exe f1.cpp f2.cpp f3.cpp … f999.cpp But only f4.cpp has changed and only needs to be recompiled! Compiler option to create intermediate (object modules) files: g++ -c f1.cpp g++ -c f2.cpp … g++ -c f999.cpp g++ -o word.exe f1.o f2.o f3.o … f999.o Then I just manually do: g++ -c f4.cpp, and then relink.

4 But how can I automatically determine only what needs to be recompiled?  Write a program that looks at the modification time of the.cpp file and the.o (or.exe) file. If the mod time of the.cpp file is more recent than the mod time of the.o (or.exe) file, then we know that we know that we need to recompile and relink.  But I don’t want to write this program! (Someone already built this into the Java compiler, and MS Visual Studio.)

5 makefiles Consist of: 1.Comments (begin with #) 2.Definitions 3.Dependencies 4.Commands

6 makefiles Consist of: 1.Comments (begin with #) 2.Definitions 3.Dependencies 4.Commands  Comments: # # this is a comment. #

7 makefiles Consist of: 1.Comments (begin with #) 2.Definitions 3.Dependencies 4.Commands  Definitions (are like constants): CC = g++ -g or CC = g++ -O2 …  To use these definitions, evaluate with $(CC).

8 3. Dependencies a: b c d …  This means that a is dependent upon (needs) b and c and d …

9 3. Dependencies a: b c d …  This means that a is dependent upon (needs) b and c and d …  What is word.exe dependent upon (below)?  g++ -o word.exe f1.o f2.o f3.o … f999.o

10 3. Dependencies a: b c d …  This means that a is dependent upon (needs) b and c and d …  What is word.exe dependent upon (below)?  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?

11 Dependencies & 4. Commands  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 (i.e., top-down). word.exe: f1.o f2.o … f999.o g++ -o word.exe f1.o f2.o … f999.o g++ -o word.exe f1.o f2.o … f999.o f1.o: f1.cpp g++ -c f1.cpp g++ -c f1.cpp f2.o: f2.cpp g++ -c f2.cpp g++ -c f2.cpp…

12 makefile syntax target1 target2 target3 : prerequisite1 prerequisite2 <tab>command1<tab>command2<tab>command3 1. One or more targets (to the left of the colon). 2. Zero or more prereqs (after the colon). 3. Zero or more commands (each must be preceeded by a tab).

13 “Standard phony” targets  all Perform all tasks to build the application  install Create an installation of the application from the compiled binaries  clean Delete the binary files generated from sources  distclean Delete all the generated files that were not in the original source distribution  TAGS Create a tags table for use by editors  info Create GNU info files from their Texinfo sources  check Run any tests associated with this application  docsCreate (doxygen) documentation. (gjg)

14 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 $(CC) -o word.exe f1.o f2.o … f999.o f1.o: f1.cpp $(CC) -c f1.cpp $(CC) -c f1.cpp…

15 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.cpp echo compile f1.cpp $(CC) -c f1.cpp … Very useful for debugging / understanding makefiles.

16 Example (make more than one target) #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.cpp echo compile f1.cpp $(CC) -c f1.cpp …

17 Example make commands  make  Checks the first dependency that appears in the file makefile.  make all  Checks the all dependency in makefile.  make word.exe  make f1.o  make fred.exe  make –f myMakeFile all  Checks the all dependency in file myMakeFile.

18 MS Visual C++ differences makenmake g++cl.o.obj no required extension for executables; MS requires.exe. -c (compile, no link)/c g++ is also linkerlink.exe is the linker (use /out:test.exe) # anywhere# must be in column 1

19

20 Project files  IDE projects + automate the creation of makefiles -user must define project -project files (and makefiles for the most part) are not portable  So utilities like cmake (cmake.org) and Ant use meta-makefiles!

21 Getting started with cmake (Linux)  We are given a folder/directory containing our source code. 1. Created a subdirectory called build.

22 Getting started with cmake (Linux)

23 2. Create a file called CMakeLists.txt.

24 Getting started with cmake (Linux) 3. Run cmake to create the Makefile.

25 Getting started with cmake (Linux)  After running cmake to create the Makefile.

26 Getting started with cmake (Linux) 4. Build the project using make.

27 Getting started with cmake (Windows) 1. Run the cmake GUI.

28 Getting started with cmake (Windows) 2. Specify the source code and build directories, and then press Configure and then Generate.

29 Getting started with cmake (Windows)  Here’s the result.

30 Getting started with cmake (Windows)  Here’s the result in Visual Studio/C++.

31 Getting started with cmake (Windows)  Building in VC++ (from menu Build --> Batch Build…).

32 Getting started with cmake (Windows)  Build result:

33 Getting started with cmake (Windows command line) 1. Use cmake.exe –G “Nmake Makefiles”.. to create the makefile for nmake.

34 Getting started with cmake (Windows command line)  Here’s the result.

35 Getting started with cmake (Windows command line) 2. Run nmake from the command line.

36 Getting started with cmake (Windows command line)  Here’s the result.


Download ppt "Makefiles. makefiles Problem: You are working on one part of a large programming project (e. g., MS Word).  It consists of hundreds of individual.cpp."

Similar presentations


Ads by Google