1 #############################################################################
3 # Generic Makefile for C/C++ Program
5 # License: GPL (General Public License)
6 # Author: whyglinux <whyglinux AT gmail DOT com>
7 # Date: 2006/03/04 (version 0.1)
8 # 2007/03/24 (version 0.2)
9 # 2007/04/09 (version 0.3)
10 # 2007/06/26 (version 0.4)
11 # 2008/04/05 (version 0.5)
15 # This is an easily customizable makefile template. The purpose is to
16 # provide an instant building environment for C/C++ programs.
18 # It searches all the C/C++ source files in the specified directories,
19 # makes dependencies, compiles and links to form an executable.
21 # Besides its default ability to build C/C++ programs which use only
22 # standard C/C++ libraries, you can customize the Makefile to build
23 # those using other libraries. Once done, without any changes you can
24 # then build programs using the same or less libraries, even if source
25 # files are renamed, added or removed. Therefore, it is particularly
26 # convenient to use it to build codes for experimental or study use.
28 # GNU make is expected to use the Makefile. Other versions of makes
29 # may or may not work.
33 # 1. Copy the Makefile to your program directory.
34 # 2. Customize in the "Customizable Section" only if necessary:
35 # * to use non-standard C/C++ libraries, set pre-processor or compiler
36 # options to <MY_CFLAGS> and linker ones to <MY_LIBS>
37 # (See Makefile.gtk+-2.0 for an example)
38 # * to search sources in more directories, set to <SRCDIRS>
39 # * to specify your favorite program name, set to <PROGRAM>
40 # 3. Type make to start building your program.
44 # The Makefile provides the following targets to make:
45 # $ make compile and link
46 # $ make NODEP=yes compile and link without generating dependencies
47 # $ make objs compile only (no linking)
48 # $ make tags create tags for Emacs editor
49 # $ make ctags create ctags for VI editor
50 # $ make clean clean objects and the executable file
51 # $ make distclean clean objects, the executable and dependencies
52 # $ make help get the usage of the makefile
54 #===========================================================================
56 ## Customizable Section: adapt those variables to suit your program.
57 ##==========================================================================
59 # The pre-processor and compiler options.
65 # The pre-processor options used by the cpp (man cpp for more).
68 # The options used in linking as well as in any direct use of ld.
71 # The directories in which source files reside.
72 # If not specified, only the current directory will be serached.
75 # The executable file name.
76 # If not specified, current directory name or `a.out' will be used.
79 ## Implicit Section: change the following only when necessary.
80 ##==========================================================================
82 # The source file types (headers excluded).
83 # .c indicates C source files, and others C++ ones.
84 SRCEXTS
= .c .C .
cc .
cpp .CPP .c
++ .
cxx .cp
86 # The header file types.
87 HDREXTS
= .h .H .hh .hpp .HPP .h
++ .hxx .hp
89 # The pre-processor and compiler options.
90 # Users can override those variables from the command line.
94 # The C program compiler.
97 # The C++ program compiler.
100 # Un-comment the following line to compile C programs as C++ ones.
103 # The command used to delete file.
112 ## Stable Section: usually no need to be changed. But you can add more.
113 ##==========================================================================
116 SPACE
= $(EMPTY
) $(EMPTY
)
118 CUR_PATH_NAMES
= $(subst /,$(SPACE
),$(subst $(SPACE
),_
,$(CURDIR
)))
119 PROGRAM
= $(word $(words $(CUR_PATH_NAMES
)),$(CUR_PATH_NAMES
))
127 SOURCES
= $(foreach d
,$(SRCDIRS
),$(wildcard $(addprefix $(d
)/*,$(SRCEXTS
))))
128 HEADERS
= $(foreach d
,$(SRCDIRS
),$(wildcard $(addprefix $(d
)/*,$(HDREXTS
))))
129 SRC_CXX
= $(filter-out %.c
,$(SOURCES
))
130 OBJS
= $(addsuffix .o
, $(basename $(SOURCES
)))
133 ## Define some useful variables.
134 DEP_OPT
= $(shell if
`$(CC) --version | grep "GCC" >/dev/null`; then \
135 echo
"-MM -MP"; else echo
"-M"; fi
)
136 DEPEND
= $(CC
) $(DEP_OPT
) $(MY_CFLAGS
) $(CFLAGS
) $(CPPFLAGS
)
137 DEPEND.d
= $(subst -g
,,$(DEPEND
))
138 COMPILE.c
= $(CC
) $(MY_CFLAGS
) $(CFLAGS
) $(CPPFLAGS
) -c
139 COMPILE.
cxx = $(CXX
) $(MY_CFLAGS
) $(CXXFLAGS
) $(CPPFLAGS
) -c
140 LINK.c
= $(CC
) $(MY_CFLAGS
) $(CFLAGS
) $(CPPFLAGS
) $(LDFLAGS
)
141 LINK.
cxx = $(CXX
) $(MY_CFLAGS
) $(CXXFLAGS
) $(CPPFLAGS
) $(LDFLAGS
)
143 .PHONY
: all objs
tags ctags
clean distclean help show
145 # Delete the default suffixes
150 # Rules for creating dependency files (.d).
151 #------------------------------------------
154 @echo
-n
$(dir $<) > $@
155 @
$(DEPEND.d
) $< >> $@
158 @echo
-n
$(dir $<) > $@
159 @
$(DEPEND.d
) $< >> $@
162 @echo
-n
$(dir $<) > $@
163 @
$(DEPEND.d
) $< >> $@
166 @echo
-n
$(dir $<) > $@
167 @
$(DEPEND.d
) $< >> $@
170 @echo
-n
$(dir $<) > $@
171 @
$(DEPEND.d
) $< >> $@
174 @echo
-n
$(dir $<) > $@
175 @
$(DEPEND.d
) $< >> $@
178 @echo
-n
$(dir $<) > $@
179 @
$(DEPEND.d
) $< >> $@
182 @echo
-n
$(dir $<) > $@
183 @
$(DEPEND.d
) $< >> $@
185 # Rules for generating object files (.o).
186 #----------------------------------------
190 $(COMPILE.c
) $< -o
$@
193 $(COMPILE.
cxx) $< -o
$@
196 $(COMPILE.
cxx) $< -o
$@
199 $(COMPILE.
cxx) $< -o
$@
202 $(COMPILE.
cxx) $< -o
$@
205 $(COMPILE.
cxx) $< -o
$@
208 $(COMPILE.
cxx) $< -o
$@
211 $(COMPILE.
cxx) $< -o
$@
213 # Rules for generating the tags.
214 #-------------------------------------
215 tags: $(HEADERS
) $(SOURCES
)
216 $(ETAGS
) $(ETAGSFLAGS
) $(HEADERS
) $(SOURCES
)
218 ctags
: $(HEADERS
) $(SOURCES
)
219 $(CTAGS
) $(CTAGSFLAGS
) $(HEADERS
) $(SOURCES
)
221 # Rules for generating the executable.
222 #-------------------------------------
224 ifeq ($(SRC_CXX
),) # C program
225 $(LINK.c
) $(OBJS
) $(MY_LIBS
) -o
$@
226 @echo Type .
/$@ to execute the program.
228 $(LINK.
cxx) $(OBJS
) $(MY_LIBS
) -o
$@
229 @echo Type .
/$@ to execute the program.
239 $(RM
) $(OBJS
) $(PROGRAM
) $(PROGRAM
).exe
246 @echo
'Generic Makefile for C/C++ Programs (gcmakefile) version 0.5'
247 @echo
'Copyright (C) 2007, 2008 whyglinux <whyglinux@hotmail.com>'
249 @echo
'Usage: make [TARGET]'
251 @echo
' all (=make) compile and link.'
252 @echo
' NODEP=yes make without generating dependencies.'
253 @echo
' objs compile only (no linking).'
254 @echo
' tags create tags for Emacs editor.'
255 @echo
' ctags create ctags for VI editor.'
256 @echo
' clean clean objects and the executable file.'
257 @echo
' distclean clean objects, the executable and dependencies.'
258 @echo
' show show variables (for debug use only).'
259 @echo
' help print this message.'
261 @echo
'Report bugs to <whyglinux AT gmail DOT com>.'
263 # Show variables (for debug use only.)
265 @echo
'PROGRAM :' $(PROGRAM
)
266 @echo
'SRCDIRS :' $(SRCDIRS
)
267 @echo
'HEADERS :' $(HEADERS
)
268 @echo
'SOURCES :' $(SOURCES
)
269 @echo
'SRC_CXX :' $(SRC_CXX
)
270 @echo
'OBJS :' $(OBJS
)
271 @echo
'DEPS :' $(DEPS
)
272 @echo
'DEPEND :' $(DEPEND
)
273 @echo
'COMPILE.c :' $(COMPILE.c
)
274 @echo
'COMPILE.cxx :' $(COMPILE.
cxx)
275 @echo
'link.c :' $(LINK.c
)
276 @echo
'link.cxx :' $(LINK.
cxx)
278 ## End of the Makefile ## Suggestions are welcome ## All rights reserved ##
279 #############################################################################