Generic Makefiles

Generic Makefiles is a collection of ready to use but powerful makefiles using GNU tools (GNU Make and GCC). They provide automatic dependencies and easy configuration for simple (flat directory) C or C++ projects.

Note that the genericity of Generic Makefiles lies in the project independence, not in environment independence. Here is a small example showing how you could try Generic Makefiles on a simple C project (in the ../../MyProject/src directory).

Example

Note that no installation is required, in this example the Generic Makefiles archive have been simply extracted to the ~/OSS/GenericMakefiles-0.3 directory.

Here is how your project may look like: three .c files, four .h files and one makefile in a ../../MyProject/src directory.

~/OSS/GenericMakefiles-0.3 $ ls -l ../../MyProject/src
total 8
-rw-rw-r--   1 Marc     Marc          184 Nov  2 21:35 1.c
-rw-rw-r--   1 Marc     Marc           45 Nov  2 21:35 1.h
-rw-rw-r--   1 Marc     Marc           54 Nov  2 21:35 2.c
-rw-rw-r--   1 Marc     Marc           45 Nov  2 21:35 2.h
-rw-rw-r--   1 Marc     Marc           39 Nov  2 21:35 3.c
-rw-rw-r--   1 Marc     Marc           61 Nov  2 21:51 3.h
-rw-rw-r--   1 Marc     Marc           46 Nov  2 21:35 g.h
-rw-rw-r--   1 Marc     Marc          130 Nov  2 21:00 makefile

And here is the magic part. Using a generic makefile from ~/OSS/GenericMakefiles-0.3, it will automatically:

~/OSS/GenericMakefiles-0.3 $ make all -f GNUmakefile_C srcdir=../../MyProject/src builddir=../../MyProject/build
GNUmakefile_C:208: ../../MyProject/build/1.d: No such file or directory
GNUmakefile_C:208: ../../MyProject/build/2.d: No such file or directory
GNUmakefile_C:208: ../../MyProject/build/3.d: No such file or directory
rm -f ../../MyProject/build/a.out ../../MyProject/build/Last_make_goal_was_* ../../MyProject/build/1.o ../../MyProject/build/2.o ../../MyProject/build/3.o ../../MyProject/build/1.d ../../MyProject/build/2.d ../../MyProject/build/3.d
touch ../../MyProject/build/Last_make_goal_was_all
/bin/sh -ec 'gcc -MM -I../../MyProject/src  ../../MyProject/src/3.c |\
sed '\''s@\(3\)\.o[ :]*@../../MyProject/build/\1.o ../../MyProject/build/3.d: $(goal_flag_file) @g'\'' > ../../MyProject/build/3.d;\
[ -s ../../MyProject/build/3.d ] || rm -f ../../MyProject/build/3.d'
/bin/sh -ec 'gcc -MM -I../../MyProject/src  ../../MyProject/src/2.c |\
sed '\''s@\(2\)\.o[ :]*@../../MyProject/build/\1.o ../../MyProject/build/2.d: $(goal_flag_file) @g'\'' > ../../MyProject/build/2.d;\
[ -s ../../MyProject/build/2.d ] || rm -f ../../MyProject/build/2.d'
/bin/sh -ec 'gcc -MM -I../../MyProject/src  ../../MyProject/src/1.c |\
sed '\''s@\(1\)\.o[ :]*@../../MyProject/build/\1.o ../../MyProject/build/1.d: $(goal_flag_file) @g'\'' > ../../MyProject/build/1.d;\
[ -s ../../MyProject/build/1.d ] || rm -f ../../MyProject/build/1.d'
gcc -c ../../MyProject/src/1.c -I../../MyProject/src -s  -Wall -Wno-uninitialized -ansi -pedantic -O3 -fomit-frame-pointer   -o ../../MyProject/build/1.o
gcc -c ../../MyProject/src/2.c -I../../MyProject/src -s  -Wall -Wno-uninitialized -ansi -pedantic -O3 -fomit-frame-pointer   -o ../../MyProject/build/2.o
gcc -c ../../MyProject/src/3.c -I../../MyProject/src -s  -Wall -Wno-uninitialized -ansi -pedantic -O3 -fomit-frame-pointer   -o ../../MyProject/build/3.o
gcc ../../MyProject/build/1.o ../../MyProject/build/2.o ../../MyProject/build/3.o -o ../../MyProject/build/a.out -s -lm 
~/OSS/GenericMakefiles-0.3 $ ../../MyProject/build/a.out
Hello 3

Now, if for instance the file 3.h is changed, only files depending on 3.h are rebuilt (1.c and 3.c).

~/OSS/GenericMakefiles-0.3 $ touch ../../MyProject/src/3.h
~/OSS/GenericMakefiles-0.3 $ make all -f GNUmakefile_C srcdir=../../MyProject/src builddir=../../MyProject/build
/bin/sh -ec 'gcc -MM -I../../MyProject/src  ../../MyProject/src/3.c |\
sed '\''s@\(3\)\.o[ :]*@../../MyProject/build/\1.o ../../MyProject/build/3.d: $(goal_flag_file) @g'\'' > ../../MyProject/build/3.d;\
[ -s ../../MyProject/build/3.d ] || rm -f ../../MyProject/build/3.d'
/bin/sh -ec 'gcc -MM -I../../MyProject/src  ../../MyProject/src/1.c |\
sed '\''s@\(1\)\.o[ :]*@../../MyProject/build/\1.o ../../MyProject/build/1.d: $(goal_flag_file) @g'\'' > ../../MyProject/build/1.d;\
[ -s ../../MyProject/build/1.d ] || rm -f ../../MyProject/build/1.d'
gcc -c ../../MyProject/src/1.c -I../../MyProject/src -s  -Wall -Wno-uninitialized -ansi -pedantic -O3 -fomit-frame-pointer   -o ../../MyProject/build/1.o
gcc -c ../../MyProject/src/3.c -I../../MyProject/src -s  -Wall -Wno-uninitialized -ansi -pedantic -O3 -fomit-frame-pointer   -o ../../MyProject/build/3.o
gcc ../../MyProject/build/1.o ../../MyProject/build/2.o ../../MyProject/build/3.o -o ../../MyProject/build/a.out -s -lm 

If you decide to use a Generic Makefile for your project, you may cp GNUmakefile_C ../../MyProject/src/GNUmakefile and then save some typing by invoking make the usual way: make all.

You may also need to change some variables in the GNUmakefile to fit your project. Here is a list of interesting variables:

User variables in Generic Makefiles
variabledefault value
CC++
EXEa.out
SOURCE_SUFFIXccc
CCgccg++
srcdir.
builddir.
INC_PATH-I$(srcdir)
LNK_LIBS-lm
COMPILE_OPTS
BWARN_OPTS-Wall -Wno-uninitialized -ansi -pedantic
EWARN_OPTS$(BWARN_OPTS) -Wchar-subscripts -Wcomment -Wimplicit -Wreturn-type -Wswitch -Wformat -Wreorder -Wtraditional -Wpointer-arith -Wbad-function-cast -Wcast-qual -Wcast-align -Wwrite-strings -Wstrict-prototypes -Wmissing-prototypes -Wredundant-decls -W -Winline $(BWARN_OPTS) -Wchar-subscripts -Wcomment -Wimplicit -Wreturn-type -Wswitch -Wformat -Wreorder -Wtemplate-debugging -Wtraditional -Wpointer-arith -Wbad-function-cast -Wcast-qual -Wcast-align -Wwrite-strings -Wstrict-prototypes -Wmissing-prototypes -Wredundant-decls -W -Winline -Woverloaded-virtual

License

This software is released under the GNU General Public License.

Download

VersionRelease dateFile
0.3 06-NOV-2000 GenericMakefiles-0.3.tar.gz
0.2 10-OCT-2000 GenericMakefiles-0.2.tar.gz

Changelog

Version 0.3

Version 0.2


© 2000 Marc Mongenet, last update and validation: 06-NOV-2000.