Guest User

Untitled

a guest
Sep 25th, 2018
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Make 8.15 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
  67.  
  68. # The options used in linking as well as in any direct use of ld.
  69. LDFLAGS   =
  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   =
  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
  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. # Rules for generating object files (.o).
  186. #----------------------------------------
  187. objs:$(OBJS)
  188.  
  189. %.o:%.c
  190.     $(COMPILE.c) $< -o $@
  191.  
  192. %.o:%.C
  193.     $(COMPILE.cxx) $< -o $@
  194.  
  195. %.o:%.cc
  196.     $(COMPILE.cxx) $< -o $@
  197.  
  198. %.o:%.cpp
  199.     $(COMPILE.cxx) $< -o $@
  200.  
  201. %.o:%.CPP
  202.     $(COMPILE.cxx) $< -o $@
  203.  
  204. %.o:%.c++
  205.     $(COMPILE.cxx) $< -o $@
  206.  
  207. %.o:%.cp
  208.     $(COMPILE.cxx) $< -o $@
  209.  
  210. %.o:%.cxx
  211.     $(COMPILE.cxx) $< -o $@
  212.  
  213. # Rules for generating the tags.
  214. #-------------------------------------
  215. tags: $(HEADERS) $(SOURCES)
  216.     $(ETAGS) $(ETAGSFLAGS) $(HEADERS) $(SOURCES)
  217.  
  218. ctags: $(HEADERS) $(SOURCES)
  219.     $(CTAGS) $(CTAGSFLAGS) $(HEADERS) $(SOURCES)
  220.  
  221. # Rules for generating the executable.
  222. #-------------------------------------
  223. $(PROGRAM):$(OBJS)
  224. ifeq ($(SRC_CXX),)              # C program
  225.     $(LINK.c)   $(OBJS) $(MY_LIBS) -o $@
  226.     @echo Type ./$@ to execute the program.
  227. else                            # C++ program
  228.     $(LINK.cxx) $(OBJS) $(MY_LIBS) -o $@
  229.     @echo Type ./$@ to execute the program.
  230. endif
  231.  
  232. ifndef NODEP
  233. ifneq ($(DEPS),)
  234.   sinclude $(DEPS)
  235. endif
  236. endif
  237.  
  238. clean:
  239.     $(RM) $(OBJS) $(PROGRAM) $(PROGRAM).exe
  240.  
  241. distclean: clean
  242.     $(RM) $(DEPS) TAGS
  243.  
  244. # Show help.
  245. help:
  246.     @echo 'Generic Makefile for C/C++ Programs (gcmakefile) version 0.5'
  247.     @echo 'Copyright (C) 2007, 2008 whyglinux <whyglinux@hotmail.com>'
  248.     @echo
  249.     @echo 'Usage: make [TARGET]'
  250.     @echo 'TARGETS:'
  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.'
  260.     @echo
  261.     @echo 'Report bugs to <whyglinux AT gmail DOT com>.'
  262.  
  263. # Show variables (for debug use only.)
  264. show:
  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)
  277.  
  278. ## End of the Makefile ##  Suggestions are welcome  ## All rights reserved ##
  279. #############################################################################
Add Comment
Please, Sign In to add comment