Advertisement
Guest User

Generic makefile

a guest
Nov 25th, 2012
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.33 KB | None | 0 0
  1. #############################################################################
  2. #
  3. # Generic Makefile for C/C++ Program
  4. #
  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)
  12. #
  13. # Description:
  14. # ------------
  15. # This is an easily customizable makefile template. The purpose is to
  16. # provide an instant building environment for C/C++ programs.
  17. #
  18. # It searches all the C/C++ source files in the specified directories,
  19. # makes dependencies, compiles and links to form an executable.
  20. #
  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.
  27. #
  28. # GNU make is expected to use the Makefile. Other versions of makes
  29. # may or may not work.
  30. #
  31. # Usage:
  32. # ------
  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.
  41. #
  42. # Make Target:
  43. # ------------
  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
  53. #
  54. #===========================================================================
  55.  
  56. ## Customizable Section: adapt those variables to suit your program.
  57. ##==========================================================================
  58.  
  59. # The pre-processor and compiler options.
  60. MY_CFLAGS =
  61.  
  62. # The linker options.
  63. MY_LIBS =
  64.  
  65. # The pre-processor options used by the cpp (man cpp for more).
  66. CPPFLAGS = -Wall -I/Library/Frameworks/SDL.framework/Headers
  67.  
  68. # The options used in linking as well as in any direct use of ld.
  69. LDFLAGS = -framework SDL -framework Cocoa -framework OpenGL
  70.  
  71. # The directories in which source files reside.
  72. # If not specified, only the current directory will be serached.
  73. SRCDIRS =
  74.  
  75. # The executable file name.
  76. # If not specified, current directory name or `a.out' will be used.
  77. PROGRAM = app
  78.  
  79. ## Implicit Section: change the following only when necessary.
  80. ##==========================================================================
  81.  
  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 .m
  85.  
  86. # The header file types.
  87. HDREXTS = .h .H .hh .hpp .HPP .h++ .hxx .hp
  88.  
  89. # The pre-processor and compiler options.
  90. # Users can override those variables from the command line.
  91. CFLAGS = -g -O2
  92. CXXFLAGS= -g -O2
  93.  
  94. # The C program compiler.
  95. #CC = gcc
  96.  
  97. # The C++ program compiler.
  98. #CXX = g++
  99.  
  100. # Un-comment the following line to compile C programs as C++ ones.
  101. #CC = $(CXX)
  102.  
  103. # The command used to delete file.
  104. #RM = rm -f
  105.  
  106. ETAGS = etags
  107. ETAGSFLAGS =
  108.  
  109. CTAGS = ctags
  110. CTAGSFLAGS =
  111.  
  112. ## Stable Section: usually no need to be changed. But you can add more.
  113. ##==========================================================================
  114. SHELL = /bin/sh
  115. EMPTY =
  116. SPACE = $(EMPTY) $(EMPTY)
  117. ifeq ($(PROGRAM),)
  118. CUR_PATH_NAMES = $(subst /,$(SPACE),$(subst $(SPACE),_,$(CURDIR)))
  119. PROGRAM = $(word $(words $(CUR_PATH_NAMES)),$(CUR_PATH_NAMES))
  120. ifeq ($(PROGRAM),)
  121. PROGRAM = a.out
  122. endif
  123. endif
  124. ifeq ($(SRCDIRS),)
  125. SRCDIRS = .
  126. endif
  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)))
  131. DEPS = $(OBJS:.o=.d)
  132.  
  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)
  142.  
  143. .PHONY: all objs tags ctags clean distclean help show
  144.  
  145. # Delete the default suffixes
  146. .SUFFIXES:
  147.  
  148. all: $(PROGRAM)
  149.  
  150. # Rules for creating dependency files (.d).
  151. #------------------------------------------
  152.  
  153. %.d:%.c
  154. @echo -n $(dir $<) > $@
  155. @$(DEPEND.d) $< >> $@
  156.  
  157. %.d:%.C
  158. @echo -n $(dir $<) > $@
  159. @$(DEPEND.d) $< >> $@
  160.  
  161. %.d:%.cc
  162. @echo -n $(dir $<) > $@
  163. @$(DEPEND.d) $< >> $@
  164.  
  165. %.d:%.cpp
  166. @echo -n $(dir $<) > $@
  167. @$(DEPEND.d) $< >> $@
  168.  
  169. %.d:%.CPP
  170. @echo -n $(dir $<) > $@
  171. @$(DEPEND.d) $< >> $@
  172.  
  173. %.d:%.c++
  174. @echo -n $(dir $<) > $@
  175. @$(DEPEND.d) $< >> $@
  176.  
  177. %.d:%.cp
  178. @echo -n $(dir $<) > $@
  179. @$(DEPEND.d) $< >> $@
  180.  
  181. %.d:%.cxx
  182. @echo -n $(dir $<) > $@
  183. @$(DEPEND.d) $< >> $@
  184.  
  185. %.d:%.m
  186. @echo -n $(dir $<) > $@
  187. @$(DEPEND.d) $< >> $@
  188.  
  189. # Rules for generating object files (.o).
  190. #----------------------------------------
  191. objs:$(OBJS)
  192.  
  193. %.o:%.c
  194. $(COMPILE.c) $< -o $@
  195.  
  196. %.o:%.C
  197. $(COMPILE.cxx) $< -o $@
  198.  
  199. %.o:%.cc
  200. $(COMPILE.cxx) $< -o $@
  201.  
  202. %.o:%.cpp
  203. $(COMPILE.cxx) $< -o $@
  204.  
  205. %.o:%.CPP
  206. $(COMPILE.cxx) $< -o $@
  207.  
  208. %.o:%.c++
  209. $(COMPILE.cxx) $< -o $@
  210.  
  211. %.o:%.cp
  212. $(COMPILE.cxx) $< -o $@
  213.  
  214. %.o:%.cxx
  215. $(COMPILE.cxx) $< -o $@
  216.  
  217. %.o:%.m
  218. $(COMPILE.cxx) $< -o $@
  219.  
  220. # Rules for generating the tags.
  221. #-------------------------------------
  222. tags: $(HEADERS) $(SOURCES)
  223. $(ETAGS) $(ETAGSFLAGS) $(HEADERS) $(SOURCES)
  224.  
  225. ctags: $(HEADERS) $(SOURCES)
  226. $(CTAGS) $(CTAGSFLAGS) $(HEADERS) $(SOURCES)
  227.  
  228. # Rules for generating the executable.
  229. #-------------------------------------
  230. $(PROGRAM):$(OBJS)
  231. ifeq ($(SRC_CXX),) # C program
  232. $(LINK.c) $(OBJS) $(MY_LIBS) -o $@
  233. @echo Type ./$@ to execute the program.
  234. else # C++ program
  235. $(LINK.cxx) $(OBJS) $(MY_LIBS) -o $@
  236. @echo Type ./$@ to execute the program.
  237. endif
  238.  
  239. ifndef NODEP
  240. ifneq ($(DEPS),)
  241. sinclude $(DEPS)
  242. endif
  243. endif
  244.  
  245. clean:
  246. $(RM) $(OBJS) $(PROGRAM) $(PROGRAM)
  247.  
  248. distclean: clean
  249. $(RM) $(DEPS) TAGS
  250.  
  251. # Show help.
  252. help:
  253. @echo 'Generic Makefile for C/C++ Programs (gcmakefile) version 0.5'
  254. @echo 'Copyright (C) 2007, 2008 whyglinux <whyglinux@hotmail.com>'
  255. @echo
  256. @echo 'Usage: make [TARGET]'
  257. @echo 'TARGETS:'
  258. @echo ' all (=make) compile and link.'
  259. @echo ' NODEP=yes make without generating dependencies.'
  260. @echo ' objs compile only (no linking).'
  261. @echo ' tags create tags for Emacs editor.'
  262. @echo ' ctags create ctags for VI editor.'
  263. @echo ' clean clean objects and the executable file.'
  264. @echo ' distclean clean objects, the executable and dependencies.'
  265. @echo ' show show variables (for debug use only).'
  266. @echo ' help print this message.'
  267. @echo
  268. @echo 'Report bugs to <whyglinux AT gmail DOT com>.'
  269.  
  270. # Show variables (for debug use only.)
  271. show:
  272. @echo 'PROGRAM :' $(PROGRAM)
  273. @echo 'SRCDIRS :' $(SRCDIRS)
  274. @echo 'HEADERS :' $(HEADERS)
  275. @echo 'SOURCES :' $(SOURCES)
  276. @echo 'SRC_CXX :' $(SRC_CXX)
  277. @echo 'OBJS :' $(OBJS)
  278. @echo 'DEPS :' $(DEPS)
  279. @echo 'DEPEND :' $(DEPEND)
  280. @echo 'COMPILE.c :' $(COMPILE.c)
  281. @echo 'COMPILE.cxx :' $(COMPILE.cxx)
  282. @echo 'link.c :' $(LINK.c)
  283. @echo 'link.cxx :' $(LINK.cxx)
  284.  
  285. ## End of the Makefile ## Suggestions are welcome ## All rights reserved ##
  286. #############################################################################
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement