向大家推薦一個C/C++通用Makefile


在使用 Makefile 之前,只需對它進行一些簡單的設置即可;而且一經設置,即使以后對源程序文件有所增減一般也不再需要改動 Makefile。因此,即便是一個沒有學習過 Makefile 書寫規則的人,也可以為自己的 C/C++ 程序快速建立一個可工作的 Makefile。 

這個 Makefile 可以在 GNU Make 和 GCC 編譯器下正常工作。但是不能保證對於其它版本的 Make 和編譯器也能正常工作。

此 Makefile 的使用方法如下:

程序目錄的組織 
  盡量將自己的源程序集中在一個目錄中,並且把 Makefile 和源程序放在一起,這樣用起來比較方便。當然,也可以將源程序分類存放在不同的目錄中。 
  在程序目錄中創建一個名為 Makefile 的文本文件,將后面列出的 Makefile 的內容復制到這個文件中。(注意:在復制的過程中,Makfile 中各命令前面的 Tab 字符有可能被轉換成若干個空格。這種情況下需要把 Makefile 命令前面的這些空格替換為一個 Tab。) 
  將當前工作目錄切換到 Makefile 所在的目錄。目前,這個 Makefile 只支持在當前目錄中的調用,不支持當前目錄和 Makefile 所在的路徑不是同一目錄的情況。

指定可執行文件 
  程序編譯和連接成功后產生的可執行文件在 Makefile 中的 PROGRAM 變量中設定。這一項不能為空。為自己程序的可執行文件起一個有意義的名子吧。

指定源程序 
  要編譯的源程序由其所在的路徑和文件的擴展名兩項來確定。由於頭文件是通過包含來使用的,所以在這里說的源程序不應包含頭文件。 
  程序所在的路徑在 SRCDIRS 中設定。如果源程序分布在不同的目錄中,那么需要在 SRCDIRS 中一一指定,並且路徑名之間用空格分隔。 
  在 SRCEXTS 中指定程序中使用的文件類型。C/C++ 程序的擴展名一般有比較固定的幾種形式:.c、.C、.cc、.cpp、.CPP、.c++、.cp、或者.cxx(參見 man gcc)。擴展名決定了程序是 C 還是 C++ 程序:.c 是 C 程序,其它擴展名表示 C++ 程序。一般固定使用其中的一種擴展名即可。但是也有可能需要使用多種擴展名,這可以在 SOURCE_EXT 中一一指定,各個擴展名之間用空格分隔。 
  雖然並不常用,但是 C 程序也可以被作為 C++ 程序編譯。這可以通過在 Makefile 中設置 CC = $(CXX) 和 CFLAGS = $(CXXFLAGS) 兩項即可實現。 
  這個 Makefile 支持 C、C++ 以及 C/C++ 混合三種編譯方式:

  如果只指定 .c 擴展名,那么這是一個 C 程序,用 $(CC) 表示的編譯命令進行編譯和連接。

  如果指定的是除 .c 之外的其它擴展名(如 .cc、.cpp、.cxx 等),那么這是一個 C++ 程序,用 $(CXX) 進行編譯和連接。
  如果既指定了 .c,又指定了其它 C++ 擴展名,那么這是 C/C++ 混合程序,將用 $(CC) 編譯其中的 C 程序,用 $(CXX) 編譯其中的 C++ 程序,最后再用 $(CXX) 連接程序。 
  這些工作都是 make 根據在 Makefile 中提供的程序文件類型(擴展名)自動判斷進行的,不需要用戶干預。

指定編譯選項 
  編譯選項由三部分組成:預處理選項、編譯選項以及連接選項,分別由 CPPFLAGS、CFLAGS與CXXFLAGS、LDFLAGS 指定。 
  CPPFLAGS 選項可參考 C 預處理命令 cpp 的說明,但是注意不能包含 -M 以及和 -M 有關的選項。如果是 C/C++ 混合編程,也可以在這里設置 C/C++ 的一些共同的編譯選項。 
  CFLAGS 和 CXXFLAGS 兩個變量通常用來指定編譯選項。前者僅僅用於指定 C 程序的編譯選項,后者僅僅用於指定 C++ 程序的編譯選項。其實也可以在兩個變量中指定一些預處理選項(即一些本來應該放在 CPPFLAGS 中的選項),和 CPPFLAGS 並沒有明確的界限。 
  連接選項在 LDFLAGS 中指定。如果只使用 C/C++ 標准庫,一般沒有必要設置。如果使用了非標准庫,應該在這里指定連接需要的選項,如庫所在的路徑、庫名以及其它聯接選項。 
  現在的庫一般都提供了一個相應的 .pc 文件來記錄使用庫所需要的預編譯選項、編譯選項和連接選項等信息,通過 pkg-config 可以動態提取這些選項。與由用戶顯式指定各個選項相比,使用 pkg-config 來訪問庫提供的選項更方便、更具通用性。在后面可以看到一個 GTK+ 程序的例子,其編譯和連接選項的指定就是用 pkg-config 實現的。

編譯和連接

  上面的各項設置好之后保存 Makefile 文件。執行 make 命令,程序就開始編譯了。 
  命令 make 會根據 Makefile 中設置好的路徑和文件類型搜索源程序文件,然后根據文件的類型調用相應的編譯命令、使用相應的編譯選項對程序進行編譯。 
  編譯成功之后程序的連接會自動進行。如果沒有錯誤的話最終會產生程序的可執行文件。 
  注意:在對程序編譯之后,會產生和源程序文件一一對應的 .d 文件。這是表示依賴關系的文件,通過它們 make 決定在源程序文件變動之后要進行哪些更新。為每一個源程序文件建立相應的 .d 文件這也是 GNU Make 推薦的方式。

Makefile 目標(Targets)

下面是關於這個 Makefile 提供的目標以及它所完成的功能:

make 
編譯和連接程序。相當於 make all。

make objs 
僅僅編譯程序產生 .o 目標文件,不進行連接(一般很少單獨使用)。

make clean 
刪除編譯產生的目標文件和依賴文件。

make cleanall 
刪除目標文件、依賴文件以及可執行文件。

make rebuild 
重新編譯和連接程序。相當於 make clean && make all。

關於這個 Makefile 的實現原理不准備詳細解釋了。如果有興趣的話,可參考文末列出的“參考資料”。

 

Makefile 的內容如下: 

C代碼   收藏代碼
  1. #############################################################  
  2. # Generic Makefile for C/C++ Program  
  3. #  
  4. # License: GPL (General Public License)  
  5. # Author:  whyglinux <whyglinux AT gmail DOT com>  
  6. # Date:    2006/03/04 (version 0.1)  
  7. #          2007/03/24 (version 0.2)  
  8. #          2007/04/09 (version 0.3)  
  9. #          2007/06/26 (version 0.4)  
  10. #          2008/04/05 (version 0.5)  
  11. #  
  12. # Description:  
  13. # ------------  
  14. # This is an easily customizable makefile template. The purpose is to  
  15. # provide an instant building environment for C/C++ programs.  
  16. #  
  17. # It searches all the C/C++ source files in the specified directories,  
  18. # makes dependencies, compiles and links to form an executable.  
  19. #  
  20. # Besides its default ability to build C/C++ programs which use only  
  21. # standard C/C++ libraries, you can customize the Makefile to build  
  22. # those using other libraries. Once done, without any changes you can  
  23. # then build programs using the same or less libraries, even if source  
  24. # files are renamed, added or removed. Therefore, it is particularly  
  25. # convenient to use it to build codes for experimental or study use.  
  26. #  
  27. # GNU make is expected to use the Makefile. Other versions of makes  
  28. # may or may not work.  
  29. #  
  30. # Usage:  
  31. # ------  
  32. # 1. Copy the Makefile to your program directory.  
  33. # 2. Customize in the "Customizable Section" only if necessary:  
  34. #    * to use non-standard C/C++ libraries, set pre-processor or compiler  
  35. #      options to <MY_CFLAGS> and linker ones to <MY_LIBS>  
  36. #      (See Makefile.gtk+-2.0 for an example)  
  37. #    * to search sources in more directories, set to <SRCDIRS>  
  38. #    * to specify your favorite program name, set to <PROGRAM>  
  39. # 3. Type make to start building your program.  
  40. #  
  41. # Make Target:  
  42. # ------------  
  43. # The Makefile provides the following targets to make:  
  44. #   $ make           compile and link  
  45. #   $ make NODEP=yes compile and link without generating dependencies  
  46. #   $ make objs      compile only (no linking)  
  47. #   $ make tags      create tags for Emacs editor  
  48. #   $ make ctags     create ctags for VI editor  
  49. #   $ make clean     clean objects and the executable file  
  50. #   $ make distclean clean objects, the executable and dependencies  
  51. #   $ make help      get the usage of the makefile  
  52. #  
  53. #===========================================================================  
  54.   
  55. ## Customizable Section: adapt those variables to suit your program.  
  56. ##==========================================================================  
  57.   
  58. # The pre-processor and compiler options.  
  59. MY_CFLAGS =  
  60.   
  61. # The linker options.  
  62. MY_LIBS   =  
  63.   
  64. # The pre-processor options used by the cpp (man cpp for more).  
  65. CPPFLAGS  = -Wall  
  66.   
  67. # The options used in linking as well as in any direct use of ld.  
  68. LDFLAGS   =  
  69.   
  70. # The directories in which source files reside.  
  71. # If not specified, only the current directory will be serached.  
  72. SRCDIRS   =  
  73.   
  74. # The executable file name.  
  75. # If not specified, current directory name or `a.out' will be used.  
  76. PROGRAM   =  
  77.   
  78. ## Implicit Section: change the following only when necessary.  
  79. ##==========================================================================  
  80.   
  81. # The source file types (headers excluded).  
  82. # .c indicates C source files, and others C++ ones.  
  83. SRCEXTS = .c .C .cc .cpp .CPP .c++ .cxx .cp  
  84.   
  85. # The header file types.  
  86. HDREXTS = .h .H .hh .hpp .HPP .h++ .hxx .hp  
  87.   
  88. # The pre-processor and compiler options.  
  89. # Users can override those variables from the command line.  
  90. CFLAGS  = -g -O2  
  91. CXXFLAGS= -g -O2  
  92.   
  93. # The C program compiler.  
  94. #CC     = gcc  
  95.   
  96. # The C++ program compiler.  
  97. #CXX    = g++  
  98.   
  99. # Un-comment the following line to compile C programs as C++ ones.  
  100. #CC     = $(CXX)  
  101.   
  102. # The command used to delete file.  
  103. #RM     = rm -f  
  104.   
  105. ETAGS = etags  
  106. ETAGSFLAGS =  
  107.   
  108. CTAGS = ctags  
  109. CTAGSFLAGS =  
  110.   
  111. ## Stable Section: usually no need to be changed. But you can add more.  
  112. ##==========================================================================  
  113. SHELL   = /bin/sh  
  114. EMPTY   =  
  115. SPACE   = $(EMPTY) $(EMPTY)  
  116. ifeq ($(PROGRAM),)  
  117.   CUR_PATH_NAMES = $(subst /,$(SPACE),$(subst $(SPACE),_,$(CURDIR)))  
  118.   PROGRAM = $(word $(words $(CUR_PATH_NAMES)),$(CUR_PATH_NAMES))  
  119.   ifeq ($(PROGRAM),)  
  120.     PROGRAM = a.out  
  121.   endif  
  122. endif  
  123. ifeq ($(SRCDIRS),)  
  124.   SRCDIRS = .  
  125. endif  
  126. SOURCES = $(foreach d,$(SRCDIRS),$(wildcard $(addprefix $(d)/*,$(SRCEXTS))))  
  127. HEADERS = $(foreach d,$(SRCDIRS),$(wildcard $(addprefix $(d)/*,$(HDREXTS))))  
  128. SRC_CXX = $(filter-out %.c,$(SOURCES))  
  129. OBJS    = $(addsuffix .o, $(basename $(SOURCES)))  
  130. DEPS    = $(OBJS:.o=.d)  
  131.   
  132. ## Define some useful variables.  
  133. DEP_OPT = $(shell if `$(CC) --version | grep "GCC" >/dev/null`; then \  
  134.                   echo "-MM -MP"; else echo "-M"; fi )  
  135. DEPEND      = $(CC)  $(DEP_OPT)  $(MY_CFLAGS) $(CFLAGS) $(CPPFLAGS)  
  136. DEPEND.d    = $(subst -g ,,$(DEPEND))  
  137. COMPILE.c   = $(CC)  $(MY_CFLAGS) $(CFLAGS)   $(CPPFLAGS) -c  
  138. COMPILE.cxx = $(CXX) $(MY_CFLAGS) $(CXXFLAGS) $(CPPFLAGS) -c  
  139. LINK.c      = $(CC)  $(MY_CFLAGS) $(CFLAGS)   $(CPPFLAGS) $(LDFLAGS)  
  140. LINK.cxx    = $(CXX) $(MY_CFLAGS) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS)  
  141.   
  142. .PHONY: all objs tags ctags clean distclean help show  
  143.   
  144. # Delete the default suffixes  
  145. .SUFFIXES:  
  146.   
  147. all: $(PROGRAM)  
  148.   
  149. # Rules for creating dependency files (.d).  
  150. #------------------------------------------  
  151.   
  152. %.d:%.c  
  153.     @echo -n $(dir $<) > $@  
  154.     @$(DEPEND.d) $< >> $@  
  155.   
  156. %.d:%.C  
  157.     @echo -n $(dir $<) > $@  
  158.     @$(DEPEND.d) $< >> $@  
  159.   
  160. %.d:%.cc  
  161.     @echo -n $(dir $<) > $@  
  162.     @$(DEPEND.d) $< >> $@  
  163.   
  164. %.d:%.cpp  
  165.     @echo -n $(dir $<) > $@  
  166.     @$(DEPEND.d) $< >> $@  
  167.   
  168. %.d:%.CPP  
  169.     @echo -n $(dir $<) > $@  
  170.     @$(DEPEND.d) $< >> $@  
  171.   
  172. %.d:%.c++  
  173.     @echo -n $(dir $<) > $@  
  174.     @$(DEPEND.d) $< >> $@  
  175.   
  176. %.d:%.cp  
  177.     @echo -n $(dir $<) > $@  
  178.     @$(DEPEND.d) $< >> $@  
  179.   
  180. %.d:%.cxx  
  181.     @echo -n $(dir $<) > $@  
  182.     @$(DEPEND.d) $< >> $@  
  183.   
  184. # Rules for generating object files (.o).  
  185. #----------------------------------------  
  186. objs:$(OBJS)  
  187.   
  188. %.o:%.c  
  189.     $(COMPILE.c) $< -o $@  
  190.   
  191. %.o:%.C  
  192.     $(COMPILE.cxx) $< -o $@  
  193.   
  194. %.o:%.cc  
  195.     $(COMPILE.cxx) $< -o $@  
  196.   
  197. %.o:%.cpp  
  198.     $(COMPILE.cxx) $< -o $@  
  199.   
  200. %.o:%.CPP  
  201.     $(COMPILE.cxx) $< -o $@  
  202.   
  203. %.o:%.c++  
  204.     $(COMPILE.cxx) $< -o $@  
  205.   
  206. %.o:%.cp  
  207.     $(COMPILE.cxx) $< -o $@  
  208.   
  209. %.o:%.cxx  
  210.     $(COMPILE.cxx) $< -o $@  
  211.   
  212. # Rules for generating the tags.  
  213. #-------------------------------------  
  214. tags: $(HEADERS) $(SOURCES)  
  215.     $(ETAGS) $(ETAGSFLAGS) $(HEADERS) $(SOURCES)  
  216.   
  217. ctags: $(HEADERS) $(SOURCES)  
  218.     $(CTAGS) $(CTAGSFLAGS) $(HEADERS) $(SOURCES)  
  219.   
  220. # Rules for generating the executable.  
  221. #-------------------------------------  
  222. $(PROGRAM):$(OBJS)  
  223. ifeq ($(SRC_CXX),)              # C program  
  224.     $(LINK.c)   $(OBJS) $(MY_LIBS) -o $@  
  225.     @echo Type ./$@ to execute the program.  
  226. else                            # C++ program  
  227.     $(LINK.cxx) $(OBJS) $(MY_LIBS) -o $@  
  228.     @echo Type ./$@ to execute the program.  
  229. endif  
  230.   
  231. ifndef NODEP  
  232. ifneq ($(DEPS),)  
  233.   sinclude $(DEPS)  
  234. endif  
  235. endif  
  236.   
  237. clean:  
  238.     $(RM) $(OBJS) $(PROGRAM) $(PROGRAM).exe  
  239.   
  240. distclean: clean  
  241.     $(RM) $(DEPS) TAGS  
  242.   
  243. # Show help.  
  244. help:  
  245.     @echo 'Generic Makefile for C/C++ Programs (gcmakefile) version 0.5'  
  246.     @echo 'Copyright (C) 2007, 2008 whyglinux <whyglinux@hotmail.com>'  
  247.     @echo  
  248.     @echo 'Usage: make [TARGET]'  
  249.     @echo 'TARGETS:'  
  250.     @echo '  all       (=make) compile and link.'  
  251.     @echo '  NODEP=yes make without generating dependencies.'  
  252.     @echo '  objs      compile only (no linking).'  
  253.     @echo '  tags      create tags for Emacs editor.'  
  254.     @echo '  ctags     create ctags for VI editor.'  
  255.     @echo '  clean     clean objects and the executable file.'  
  256.     @echo '  distclean clean objects, the executable and dependencies.'  
  257.     @echo '  show      show variables (for debug use only).'  
  258.     @echo '  help      print this message.'  
  259.     @echo  
  260.     @echo 'Report bugs to <whyglinux AT gmail DOT com>.'  
  261.   
  262. # Show variables (for debug use only.)  
  263. show:  
  264.     @echo 'PROGRAM     :' $(PROGRAM)  
  265.     @echo 'SRCDIRS     :' $(SRCDIRS)  
  266.     @echo 'HEADERS     :' $(HEADERS)  
  267.     @echo 'SOURCES     :' $(SOURCES)  
  268.     @echo 'SRC_CXX     :' $(SRC_CXX)  
  269.     @echo 'OBJS        :' $(OBJS)  
  270.     @echo 'DEPS        :' $(DEPS)  
  271.     @echo 'DEPEND      :' $(DEPEND)  
  272.     @echo 'COMPILE.c   :' $(COMPILE.c)  
  273.     @echo 'COMPILE.cxx :' $(COMPILE.cxx)  
  274.     @echo 'link.c      :' $(LINK.c)  
  275.     @echo 'link.cxx    :' $(LINK.cxx)  
  276.   
  277. ## End of the Makefile ##  Suggestions are welcome  ## All rights reserved ##  
  278. ##############################################################  

 

 

 

下面提供兩個例子來具體說明上面 Makefile 的用法。 

例一 Hello World 程序

這個程序的功能是輸出 Hello, world! 這樣一行文字。由 hello.h、hello.c、main.cxx 三個文件組成。前兩個文件是 C 程序,后一個是 C++ 程序,因此這是一個 C 和 C++ 混編程序。

C代碼   收藏代碼
  1. /* File name: hello.h 
  2.  * C header file 
  3.  */  
  4.   
  5. #ifndef HELLO_H  
  6. #define HELLO_H  
  7.   
  8. #ifdef __cplusplus  
  9. extern "C" {  
  10. #endif  
  11.   
  12.   void print_hello();  
  13. #ifdef __cplusplus  
  14. }  
  15.   
  16. #endif  
  17. #endif  
  18.   
  19.   
  20. /* File name: hello.c 
  21.  * C source file. 
  22.  */  
  23.   
  24. #include "hello.h"  
  25. #include <stdio.h>  
  26.   
  27. void print_hello()  
  28. {  
  29.   puts( "Hello, world!" );  
  30. }  
  31.   
  32.   
  33. /* File name: main.cxx 
  34.  * C++ source file. 
  35.  */  
  36.   
  37. #include "hello.h"  
  38.   
  39. int main()  
  40. {  
  41.   print_hello();  
  42.   return 0;  
  43. }  

 

 

建立一個新的目錄,然后把這三個文件拷貝到目錄中,也把 Makefile 文件拷貝到目錄中。之后,對 Makefile 的相關項目進行如下設置: 

PROGRAM   := hello      # 設置運行程序名

SRCDIRS   := .          # 源程序位於當前目錄下

 

SRCEXTS   := .c .cxx    # 源程序文件有 .c 和 .cxx 兩種類型

CFLAGS    := -g         # 為 C 目標程序包含 GDB 可用的調試信息

CXXFLAGS  := -g         # 為 C++ 目標程序包含 GDB 可用的調試信息 

  由於這個簡單的程序只使用了 C 標准庫的函數(puts),所以對於 CFLAGS 和 CXXFLAGS 沒有過多的要求,LDFLAGS 和 CPPFLAGS 選項也無需設置。 

  經過上面的設置之后,執行 make 命令就可以編譯程序了。如果沒有錯誤出現的話,./hello  就可以運行程序了。

  如果修改了源程序的話,可以看到只有和修改有關的源文件被編譯。也可以再為程序添加新的源文件,只要它們的擴展名是已經在 Makefile 中設置過的,那么就沒有必要修改 Makefile。 

 

例二 GTK+ 版 Hello World 程序

  這個 GTK+ 2.0 版的 Hello World 程序可以從下面的網址上得到:http://www.gtk.org/tutorial/c58.html#SEC-HELLOWORLD。當然,要編譯 GTK+ 程序,還需要你的系統上已經安裝好了 GTK+。 
  跟第一個例子一樣,單獨創建一個新的目錄,把上面網頁中提供的程序保存為 main.c 文件。對 Makefile 做如下設置: 
PROGRAM   := hello      # 設置運行程序名
SRCDIRS   := .          # 源程序位於當前目錄下
SRCEXTS   := .c         # 源程序文件只有 .c 一種類

CFLAGS    := `pkg-config --cflags gtk+-2.0`  # CFLAGS
LDFLAGS   := `pkg-config --libs gtk+-2.0`    # LDFLAGS

這是一個 C 程序,所以 CXXFLAGS 沒有必要設置——即使被設置了也不會被使用。 
編譯和連接 GTK+ 庫所需要的 CFLAGS 和 LDFLAGS 由 pkg-config 程序自動產生。 
現在就可以運行 make 命令編譯、./hello 執行這個 GTK+ 程序了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM