Benjamin

静以修身,俭以养德,非澹薄无以明志,非宁静无以致远。
随笔 - 386, 文章 - 0, 评论 - 196, 引用 - 0
数据加载中……

linux常用makefile模板

 

 一、通用的编译c、c++单工程makefile模板。
1、MY_LIBS是添加静态库选项
2、MY_CFLAGS一般添加include文件路径,这个路径被用在了c和c++文件编译时,对c和c++都有效;如果想分开控制,请定义两个类似的变量
3、PROGRAM是最终生成的文件名,默认的是文件夹名字,也可自己指定
4、如果一个项目有多个工程,在顶层执行这个文件,会导致进入这个文件夹两次,第一次产生.d依赖文件,由于此时.o文件尚未生成,所以什么也没做,
第二次进入这个目录,会生成o文件,最终连接成可执行文件。.d(depend)依赖文件;如果以后在编译时,请不要删除这些.d文件,以后有了改动,
比如更改了.h文件中的宏变量,则此时可根据.depend中的依赖关系只生成对应的.o文件了。总之,.d文件可要可不要。默认的,*.d依赖文件不删除。
不需要d文件,将sinclude $(DEPS)注释掉即可
5、如果源文件在别的目录,请设置VPATH

  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------
 321. Copy the Makefile to your program directory.
 332. 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>
 393. 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.
 59MY_CFLAGS =
 60
 61# The linker options.
 62MY_LIBS   =
 63
 64# The pre-processor options used by the cpp (man cpp for more).
 65CPPFLAGS  = -Wall
 66
 67# The options used in linking as well as in any direct use of ld.
 68LDFLAGS   =
 69
 70# The directories in which source files reside.
 71# If not specified, only the current directory will be serached.
 72SRCDIRS   =
 73
 74# The executable file name.
 75# If not specified, current directory name or `a.out' will be used.
 76PROGRAM   =
 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.
 83SRCEXTS = .c .C .cc .cpp .CPP .c++ .cxx .cp
 84
 85# The header file types.
 86HDREXTS = .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.
 90CFLAGS  = --O2
 91CXXFLAGS= --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
105ETAGS = etags
106ETAGSFLAGS =
107
108CTAGS = ctags
109CTAGSFLAGS =
110
111## Stable Section: usually no need to be changed. But you can add more.
112##==========================================================================
113SHELL   = /bin/sh
114EMPTY   =
115SPACE   = $(EMPTY) $(EMPTY)
116ifeq ($(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
122endif
123ifeq ($(SRCDIRS),)
124  SRCDIRS = .
125endif
126SOURCES = $(foreach d,$(SRCDIRS),$(wildcard $(addprefix $(d)/*,$(SRCEXTS))))
127HEADERS = $(foreach d,$(SRCDIRS),$(wildcard $(addprefix $(d)/*,$(HDREXTS))))
128SRC_CXX = $(filter-out %.c,$(SOURCES))
129OBJS    = $(addsuffix .o, $(basename $(SOURCES)))
130DEPS    = $(OBJS:.o=.d)
131
132## Define some useful variables.
133DEP_OPT = $(shell if `$(CC) --version | grep "GCC" >/dev/null`; then \
134                  echo "-MM -MP"; else echo "-M"; fi )
135DEPEND      = $(CC)  $(DEP_OPT)  $(MY_CFLAGS) $(CFLAGS) $(CPPFLAGS)
136DEPEND.d    = $(subst -g ,,$(DEPEND))
137COMPILE.c   = $(CC)  $(MY_CFLAGS) $(CFLAGS)   $(CPPFLAGS) -c
138COMPILE.cxx = $(CXX) $(MY_CFLAGS) $(CXXFLAGS) $(CPPFLAGS) -c
139LINK.c      = $(CC)  $(MY_CFLAGS) $(CFLAGS)   $(CPPFLAGS) $(LDFLAGS)
140LINK.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
147all: $(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#----------------------------------------
186objs:$(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#-------------------------------------
214tags: $(HEADERS) $(SOURCES)
215 $(ETAGS) $(ETAGSFLAGS) $(HEADERS) $(SOURCES)
216
217ctags: $(HEADERS) $(SOURCES)
218 $(CTAGS) $(CTAGSFLAGS) $(HEADERS) $(SOURCES)
219
220# Rules for generating the executable.
221#-------------------------------------
222$(PROGRAM):$(OBJS)
223ifeq ($(SRC_CXX),)              # C program
224 $(LINK.c)   $(OBJS) $(MY_LIBS) -o $@
225 @echo Type ./$@ to execute the program.
226else                            # C++ program
227 $(LINK.cxx) $(OBJS) $(MY_LIBS) -o $@
228 @echo Type ./$@ to execute the program.
229endif
230
231ifndef NODEP
232ifneq ($(DEPS),)
233  sinclude $(DEPS)
234endif
235endif
236
237clean:
238 $(RM) $(OBJS) $(PROGRAM) $(PROGRAM).exe
239
240distclean: clean
241 $(RM) $(DEPS) TAGS
242
243# Show help.
244help:
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.)
263show:
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##############################################################
279
280

二、多项目工程(顶层)makefile模板:这里采用遍历子文件夹,在有Makefile文件的文件夹下执行make命令
1、make  ver=$(ver)这里是版本控制,比如make ver=r就是release版本,默认是debug,
版本控制也需要在子项目的Makefile文件中体现,这是由用户自己定义控制
2、make默认是打开w,输出运行makefile之前和之后的信息,可以用--no-print-directory禁止输出这个trace信息
例如:如果采用上面的c、c++工程makefile模板,屏幕会打印进出文件夹的信息,通过这选项可以禁止输出这些信息。
3、这里检测的是各个文件夹下的Makefile文件是否存在,也可以检测其他文件。

 1# Top Makefile 
 2DIR = /software/project    #也可以DIR:= `ls .`
 3MODULES = $(shell ls $(DIR)) #也可以MODULES:=`pwd`
 4
 5all : $(MODULES)
 6 @for subdir in $(MODULES); \
 7 do \
 8 if test -f $(DIR)/$$subdir/Makefile;then\
 9  cd $(DIR)/$$subdir;\
10 make  ver=$(ver) ;\
11 cd  $(DIR);\
12 fi;\
13done
14
15clean :
16 @for subdir in $(MODULES); \
17do \
18 if test -f $(DIR)/$$subdir/Makefile;then\
19  cd $(DIR)/$$subdir;\
20 make --no-print-directory clean ver=$(ver) ;\
21 cd  $(DIR);\
22 fi;\
23done
24
25tags:
26 ctags -R
27
28
29.PHONY : all clean distclean tags help
30
31


三、多项目工程(顶层)makefile模板,采用路径来表示,不用再检测是否有makefile文件;这边版本可以灵活的添加

 1# Top Makefile for C program
 2
 3all :
 4 #version control.default is debug
 5
 6 $(MAKE) ver=$(ver) -/home/project/baseframe 
 7 $(MAKE) ver=$(ver) -/home/project/logic  
 8 $(MAKE) ver=$(ver) -C  /home/project/login 
 9 $(MAKE) ver=$(ver) -/home/project/db 
10tags:
11 ctags -R
12
13help:
14 @echo "=====================A common Makefilefor c programs===================="
15 @echo "Copyright (C) 2014"
16 @echo "The following targets aresupport:"
17 @echo
18 @echo " all              - (==make) compile and link"
19 @echo " obj              - just compile, withoutlink"
20 @echo " clean            - clean target"
21 @echo " distclean        - clean target and otherinformation"
22 @echo " tags             - create ctags for vimeditor"
23 @echo " help             - print help information"
24 @echo
25 @echo "To make a target, do 'make[target]'"
26 @echo "========================= Version2.0 ==================================="
27
28clean :
29
30 $(MAKE) ver=$(ver) -/home/project/baseframe   clean
31 $(MAKE) ver=$(ver) -/home/project/logic   clean
32 $(MAKE) ver=$(ver) -/home/project/Login clean 
33 $(MAKE) ver=$(ver) -/home/project/db    clean 
34
35distclean:
36 $(MAKE) -/home/project/baseframe distclean
37 $(MAKE) -/home/project/logic distclean
38 $(MAKE) -/home/project/Login distclean
39 $(MAKE) -/home/project/db distclean
40
41  
42.PHONY : all clean distclean tags help
43


 四、通用的c、c++混合编译模板

  1#
  2# c.cpp混合编译的makefile模板
  3#
  4#
  5
  6BIN = liblua.so
  7CC = gcc
  8CPP = g++
  9#系统库的包含路径、库列表
 10INCS = 
 11LIBS = 
 12SUBDIRS =
 13
 14#源文件包含路径、库列表
 15SOURCEINC = 
 16#
 17#
 18#maintest.c tree/rbtree.c  多了子目录,那就直接添加 目录/*.c即可   所有的源文件--  .c文件列表
 19CSRCS = $(wildcard  ./*.c )
 20CPPSRCS = $(wildcard ./*.cpp)
 21
 22#
 23#
 24#所有的.o文件列表
 25COBJS := $(CSRCS:.c=.o)
 26CPPOBJS := $(CPPSRCS:.cpp=.o)
 27#
 28#生成依赖信息 -MM是只生成自己的头文件信息,-M 包含了标准库头文件信息。
 29#-MT 或 -MQ都可以改变生成的依赖  xxx.o:src/xxx.h 为 src/xxx.o:src/xxx.h 当然。前面的 src/xxx.o需自己指定
 30#格式为 -MM 输入.c或.cpp  查找依赖路径  -MT或-MQ  生成规则,比如src/xxx.o 
 31MAKEDEPEND = gcc -MM -MT
 32CFLAGS =
 33CPPFLAGS =
 34
 35#-g 生成调试信息
 36#-pedantic参数与-ansi一起使用 会自动拒绝编译非ANSI程序
 37#-fomit-frame-pointer 去除函数框架
 38#-Wmissing-prototypes -Wstrict-prototypes 检查函数原型
 39#针对每个.c文件的.d依赖文件列表
 40CDEF = $(CSRCS:.c=.d)
 41CPPDEF = $(CPPSRCS:.cpp=.d)
 42
 43PLATS = win32-debug win32-release linux-debug linux-release
 44none:
 45 @echo "Please choose a platform:"
 46 @echo " $(PLATS)"
 47
 48win32-debug:
 49 $(MAKE) all INCS=-I"c:/mingw/include" LIBS=-L"c:/mingw/lib" CFLAGS="-Wall -DWIN32 -DDEBUG -g" CPPFLAGS="-Wall -DWIN32 -DDEBUG -g"
 50
 51win32-release:
 52 $(MAKE) all INCS=-I"c:/mingw/include" LIBS=-L"c:/mingw/lib" CFLAGS="-Wall -DWIN32 -DNDEBUG -O2" CPPFLAGS="-Wall -DWIN32 -DNDEBUG -O2"
 53 
 54linux-debug:
 55 $(MAKE) all INCS=-I"/usr/include" LIBS=-L"/usr/lib" CFLAGS="-fPIC -Wall -DDEBUG -g" CPPFLAGS="-fPIC -Wall -DDEBUG -g" -lpthread
 56
 57linux-release:
 58 $(MAKE) all INCS=-I"/usr/include" LIBS=-L"/usr/lib" CFLAGS="-fPIC -Wall -DNDEBUG -O2" CPPFLAGS="-fPIC -Wall -DNDEBUG -O2" -lpthread
 59
 60all:$(BIN)
 61
 62#$(OBJS):%.o :%.c  先用$(OBJS)中的一项,比如foo.o: %.o : %.c  含义为:试着用%.o匹配foo.o。如果成功%就等于foo。如果不成功,
 63# Make就会警告,然后。给foo.o添加依赖文件foo.c(用foo替换了%.c里的%)
 64# 也可以不要下面的这个生成规则,因为下面的 include $(DEF)  就隐含了。此处为了明了,易懂。故留着
 65$(COBJS) : %.o: %.c
 66 $(CC) -c $< -o $@ $(INCS) $(SOURCEINC) $(CFLAGS)
 67$(CPPOBJS) : %.o: %.cpp
 68 $(CPP) -c $< -o $@ $(INCS) $(SOURCEINC) $(CPPFLAGS)
 69
 70# $@--目标文件,$^--所有的依赖文件,$<--第一个依赖文件。每次$< $@ 代表的值就是列表中的
 71#
 72
 73####################################
 74#最终链接目标是.a(静态库)
 75$(BIN) : $(COBJS) $(CPPOBJS)
 76 ar r $@  $(COBJS) $(CPPOBJS) $(LIBS)
 77 ranlib $(BIN)
 78 -rm $(COBJS) $(CPPOBJS)
 79
 80####################################
 81#如果不加-fPIC,则加载.so文件的代码段时,造成每个使用这个.so文件代码段的进程在内核里都会生成这个.so文件代码段的copy.每个copy都不一样,取决于 这个.so文件代码段和数据段内存映射的位置.
 82#使用-fPIC,则产生的代码中,没有绝对地址,全部使用相对地址,故而代码可以被加载器加载到内存的任意位置,都可以正确的执行
 83#最终连接目标是.so(动态库)
 84#$(BIN) : $(COBJS) $(CPPOBJS)
 85#-shared -fPIC-o $(BIN) $(COBJS) $(CPPOBJS) $(LIBS)
 86#-rm $(COBJS) $(CPPOBJS)
 87
 88####################################
 89#最终链接目标是可执行文件
 90$(BIN) : $(COBJS) $(CPPOBJS)
 91 $(CPP) -o $(BIN) $(COBJS) $(CPPOBJS) $(LIBS)
 92 -rm $(COBJS) $(CPPOBJS)
 93# 链接为最终目标
 94
 95
 96#引入了.o文件对.c和.h的依赖情况。以后.h被修改也会重新生成,可看看.d文件内容即知道为何
 97#引入了依赖就相当于引入了一系列的规则,因为依赖内容例如: 目录/xxx.o:目录/xxx.c 目录/xxx.h 也相当于隐含的引入了生成规则
 98#故上面不能在出现如: $(OBJS) : $(DEF)之类。切记
 99#include $(CDEF) $(CPPDEF)
100.PHONY:clean cleanall
101
102#清除所有目标文件以及生成的最终目标文件
103clean:   
104 -rm $(BIN) $(COBJS) $(CPPOBJS)
105#rm *.d
106cleanall:
107 -rm $(BIN) $(COBJS) $(CPPOBJS)
108
109五、简单makefile模板:只有一个.o文件
110#/***
111#
112#Copyright 2006 bsmith@qq.com
113#
114#Licensed under the Apache License, Version 2.0 (the "License");
115#you may not use this file except in compliance with the License.
116#You may obtain a copy of the License at
117#
118#   http://www.apache.org/licenses/LICENSE-2.0
119#
120#Unless required by applicable law or agreed to in writing, software
121#distributed under the License is distributed on an "AS IS" BASIS,
122#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123#See the License for the specific language governing permissions and
124#limitations under the License.
125#
126#*/

127
128============project================
129PROJECT = db
130AUTHOR = bsmith
131HOME = .
132=================================
133
134==============bin===================
135TARGET = ../bin/db_d
136===================================
137
138========================================
139===========这里可以作为工程的include文件夹,所有的头文件都可以放到这里===#
140IFLAGS =  -I"../common/luainclude/" -I"../common/net/" -I"../common/base/" -I"../common/tool/"  -I"../common/" -I"../common/tool/mysql/" -I"../common/luainclude/LuaBridge" -I"../common/luainclude/LuaBridge/detail" -I"../common/Act/" -I"../common/luainclude/LLua"
141===============config====================
142CC = g++
143CFLAGS = -w  -ldl -lrt -Wno-deprecated-declarations
144LFLAGS =  ../common/lib/liblxnet.a   ../common/lib/libxml.a ../common/lib/libbaseframe.a ../common/lib/libjemalloc.a   ../common/lib/liblua52.a -lpthread -lmysqlclient
145========================================
146#maintest.c tree/rbtree.c  多了子目录,那就直接添加 目录/*.c即可   所有的源文件--  .c文件列表  
147CSRCS = $(wildcard  ./*.c )  
148CPPSRCS = $(wildcard ./*.cpp )
149#所有的.o文件列表  
150COBJS := $(CSRCS:.c=.o)  
151CPPOBJS := $(CPPSRCS:.cpp=.o)  
152
153# args
154# [args] 生成模式. 0代表debug模式, 1代表release模式. make ver=r.
155ifeq ($(ver),r)
156    # release
157    CFLAGS +=  -O3
158    TARGET = ../bin/db_r
159else
160 # debug
161    CFLAGS += -g
162endif
163
164# ==================compile====================
165%.o : %.cpp 
166  $(CC) -g -c $(CFLAGS) $(IFLAGS) $(LFLAGS) $< -o $@ 
167 
168all : bin
169
170bin : $(TARGET)
171
172$(TARGET) : $(OBJS)
173 $(CC) $(CPPSRCS) -o $(TARGET) $(OBJS)  $(IFLAGS) $(LFLAGS) $(CFLAGS)
174
175install : $(TARGET)
176  @cp $(TARGET) $(HOME)/bin/
177  @ln -s $(HOME)/bin/$(TARGET) $(HOME)/bin/$(LINK) 
178  
179clean :
180 @rm -f $(OBJS)
181 @rm -f ../bin/$(TARGET)
182 @rm -f $(HOME)/bin/$(TARGET)
183 @rm -f $(HOME)/bin/$(LINK)
184 @rm -f ./*.o
185distclean:
186 @rm -f ./bin/$(TARGET)
187# ==========================================
188
189
190 
191


 

posted on 2015-12-31 20:11 Benjamin 阅读(3350) 评论(0)  编辑 收藏 引用 所属分类: linux


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理