CSCI 2720 Data Structures Spring 2004 ****** Sample g++ makefile for code using templates ****** Below is a small sample makefile which compiles a test program for a templated set class. Notice that a separate object file for the set class is NOT created for the actual test program, becuase g++ would not be able to link it to the test program because of the templates. However a separate compile-only line is provided for the object file 'set.o' in order to aid in debugging; you should work on 'set.h' and 'set.cc' until 'set.o' compiles without errors before attempting to compile 'stest.cc'. If the name of the file is "makefile" and you execute the command "make" then it will recompile stest.cc with set.cc together if stest is not up to date. The first line in each pair shows dependencies: stest must be remade if stest.cc, set.cc or set.h has been changed after the last time stest was made. The second line in each pair always has to START WITH A TAB (the method line with the g++ etc.) If you execute "make set.o" then it will only recompile set.cc into set.o if it is not up to date (i.e., if you changed set.cc or set.h since last compiling to set.o). In order for this scheme to work, your #include files must be organized as follows: set.cc should #include "set.h" stest.cc should #include "set.cc". Note that for the stest method line, the flag/parameter pair '-o stest' ensures that the executable file is named 'stest'; without that, the name would be 'a.out'. There is no such flag/parameter pair in the set.o method line, as the default name of the object file is what we want, 'set.o'. WARNING: it is easy to delete your source code using the naming flag -o, say by typing 'g++ -o stest.cc stest', for make deletes any existing file of the name given after '-o' before attempting the compile; in this case, stest.cc is erased and then a compile error occurs because stest can't be found, or contains no 'main( )', or whatever. To guard against this sort of accident, you should always do 2 things: 1. Run all compiles via 'make', thus avoiding most typing errors. 2. Maintain a backup copy of your source code at all times. The -g flag allows you to use one of the g++ debuggers, gdb or xgdb. When you compile for timing tests, replace the flag -g with -O3. The latter optimizes the executable code for run-time speed. ------- Sample makefile below this line. -------------- stest: stest.cc set.cc set.h g++ -g -o stest stest.cc set.o: set.cc set.h g++ -g -c set.cc