INTRODUCING MAKEFILES For CS 302 Data Structures Course














- Slides: 14

INTRODUCING MAKEFILES For CS 302 Data Structures Course Slides prepared by TALHA OZ (most of the text is from http: //www. cs. uregina. ca/Links/class-info/170/fong-compile/fong. html)

MAKEFILES A description file that defines the relationships or dependencies between applications and functions; it simplifies the development process by automatically performing tasks necessary to rebuild an application when you modify code. * *: http: //www. ssec. wisc. edu/mcidas/do c/prog_man/2003/glossary. html#16 906 Benefits How-to Calculator Example

BENEFITS

Separate Compilation You would have to compile each of these files separately to produce. o files and then link them all together. The following commands show to do this. Notice the -c option you need to specify when you want to compile only , not to compile and link. CPP & Header Files

Time Consuming… Compile only: g++ -c main. cpp Compile only: g++ -c myfunction. cpp Link: g++ main. o myfunction. o -o main Note that if you wanted to compile a whole set of C++ programs at the same time, you could enter: g++ -c *. cpp Be careful with this though, for these reasons: - Using the wildcard "*" will refer to every. cpp file in the current directory. If you are going to use this, you should first put all the files for the program in a special directory for that program. - If you have only made a change to one file, there is no reason to compile every single file. In a program that consists of many files, this is extremely time-consuming.

Compiling & Linking Note that to link a whole set of object files you could have just entered: g++ *. o -o main Again, you should be careful that you have all your files in one directory. The following diagram illustrates how the previous example appears conceptually:

HOW-TO BUILD EXECUTABLES WITH "MAKE"

Without Makefile scenerio > g++ -c Calculatr. cpp > g++ -c Do. Menu. cpp Compile > g++ -c Get. Operands. cpp > g++ -c Add. Nums. cpp > g++ -c Mod. Nums. cpp > g++ -c Expon. cpp Recompile Executable > g++ Calculatr. o Do. Menu. o Get. Operands. o Add. Nums. o Mod. Nums. o Expon. o -o Calculatr Either recompile all (TIME CONSUMING) OR Just those are changed & their dependents (THIS ALSO TAKES YOUR TIME) Re-link Link

Makefile Process Comment Target Dependencies Command

The target of your action (e. g. Calculatr. o) The dependencies for the target (e. g. Calculatr. cpp and Calculatr. h) For each action you need to perform, you need to specify And on the next line, the command you want to perform Each command must begin with a <tab> character

MAKEFILE TO CREATE THE CALCULATR PROJECT

Inside of a Makefile File

How to Use It: simply enter make target at the system prompt

THE END