码迷,mamicode.com
首页 > 系统相关 > 详细

linux Makefile(中文版1)

时间:2015-09-06 21:22:37      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:

#############################################################################
# Generic Makefile for C/C++ Program
# 用法:
# ------
# 1. 拷贝Makefile到你的代码目录下
# 2. 只有在必要时"Customizable Section"自定义
# * 为了使用非标准库,设置预编译器和编译器<MY_CFLAGS>,链接器<MY_LIBS>
# * 为了搜索更多目录下的源代码,设置<SRCDIRS>
# * 指定你喜欢的程序名,设置<PROGRAM>
# 3. 打make开始编译你的程序
#
# Make Target:
# ------------
# The Makefile provides the following targets to make:
# $ make compile and link
# $ make NODEP=yes compile and link without generating dependencies
# $ make objs compile only (no linking)
# $ make tags create tags for Emacs editor
# $ make ctags create ctags for VI editor
# $ make clean clean objects and the executable file
# $ make distclean clean objects, the executable and dependencies
# $ make help get the usage of the makefile
#
#===========================================================================

## Customizable Section: adapt those variables to suit your program.
##==========================================================================

# 预编译器和编译器选项
# MY_CFLAGS = -ggdb3 -pipe -O2 -Wall -Wextra -fopenmp -march=native -mfpmath=sse -DLINUX -m64 -std=c++0x
MY_CFLAGS = -g -DLINUX -Itest1/include -Itest2/include -Itest1/include/test1 -Itest2/include/test2

# 链接器选项,貌似又写成LIBS,如LIBS = -lmysqlclient -liconv,指定要链接的库
# MY_LIBS = -lGLEW -lglut -lGLU -lGL -lX11 -lXmu -lXi -lm -L/usr/X11R6/lib -lgomp -lOpenThreads -lpthread
MY_LIBS = -lm

# cpp使用的预编译器,如CPPFLAGS=‘-I/usr/local/libjpeg/include -I/usr/local/libpng/include‘ (man cpp for more).
CPPFLAGS =

# 链接器选项和链接器ld怎么用,如LDFLAGS = -L/var/xxx/lib -L/opt/mysql/lib -Wl,R/var/xxx/lib -Wl,R/opt/mysql/lib
LDFLAGS =

# 源文件目录
# If not specified, only the current directory will be serached.
SRCDIRS =


# 可执行文件名
# If not specified, current directory name or `a.out‘ will be used.
PROGRAM =

## Implicit Section: 必要时修改
##==========================================================================

# 源文件类型(headers excluded).
# .c是C语言文件,其它的是C++文件.
SRCEXTS = .c .C .cc .cpp .CPP .c++ .cxx .cp

# 头文件类型
HDREXTS = .h .H .hh .hpp .HPP .h++ .hxx .hp

# 预编译器和编译器选项
# 用户可以在命令行中重写这些变量U
CFLAGS =
# CXXFLAGS= -std=c++0x
CXXFLAGS=
# C程序编译器
CC = gcc

# C++程序编译器
CXX = g++

# 取消注释下面一行,C程序将被当成C++编译
#CC = $(CXX)

# 删除文件命令
RM = rm -f

ETAGS = etags
ETAGSFLAGS =

CTAGS = ctags
CTAGSFLAGS =

## Stable Section:一般不需要改变.但你可以增加更多
##==========================================================================
SHELL = /bin/sh
EMPTY =
SPACE = $(EMPTY) $(EMPTY)
ifeq ($(PROGRAM),)
CUR_PATH_NAMES = $(subst /,$(SPACE),$(subst $(SPACE),_,$(CURDIR)))
PROGRAM = $(word $(words $(CUR_PATH_NAMES)),$(CUR_PATH_NAMES))
ifeq ($(PROGRAM),)
PROGRAM = a.out
endif
endif
ifeq ($(SRCDIRS),)
SRCDIRS = .
endif
SOURCES = $(foreach d,$(SRCDIRS),$(wildcard $(addprefix $(d)/*,$(SRCEXTS))))
HEADERS = $(foreach d,$(SRCDIRS),$(wildcard $(addprefix $(d)/*,$(HDREXTS))))
SRC_CXX = $(filter-out %.c,$(SOURCES))
OBJS = $(addsuffix .o, $(basename $(SOURCES)))
DEPS = $(OBJS:.o=.d)

## 定义一些有用的变量
DEP_OPT = $(shell if `$(CC) --version | grep "GCC" >/dev/null`; then \
echo "-MM -MP"; else echo "-M"; fi )
DEPEND = $(CC) $(DEP_OPT) $(MY_CFLAGS) $(CFLAGS) $(CPPFLAGS)
DEPEND.d = $(subst -g ,,$(DEPEND))
COMPILE.c = $(CC) $(MY_CFLAGS) $(CFLAGS) $(CPPFLAGS) -c
COMPILE.cxx = $(CXX) $(MY_CFLAGS) $(CXXFLAGS) $(CPPFLAGS) -c
LINK.c = $(CC) $(MY_CFLAGS) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS)
LINK.cxx = $(CXX) $(MY_CFLAGS) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS)

.PHONY: all objs tags ctags clean distclean help show

# 删除一些默认的后缀
.SUFFIXES:

all: $(PROGRAM)

# 创建依赖文件的规则(.d).
#------------------------------------------

%.d:%.c
@echo -n $(dir {1}lt;) > $@
@$(DEPEND.d) {1}lt; >> $@

%.d:%.C
@echo -n $(dir {1}lt;) > $@
@$(DEPEND.d) {1}lt; >> $@

%.d:%.cc
@echo -n $(dir {1}lt;) > $@
@$(DEPEND.d) {1}lt; >> $@

%.d:%.cpp
@echo -n $(dir {1}lt;) > $@
@$(DEPEND.d) {1}lt; >> $@

%.d:%.CPP
@echo -n $(dir {1}lt;) > $@
@$(DEPEND.d) {1}lt; >> $@

%.d:%.c++
@echo -n $(dir {1}lt;) > $@
@$(DEPEND.d) {1}lt; >> $@

%.d:%.cp
@echo -n $(dir {1}lt;) > $@
@$(DEPEND.d) {1}lt; >> $@

%.d:%.cxx
@echo -n $(dir {1}lt;) > $@
@$(DEPEND.d) {1}lt; >> $@

# 生成目标文件的规则(.o).
#----------------------------------------
objs:$(OBJS)

%.o:%.c
$(COMPILE.c) {1}lt; -o $@

%.o:%.C
$(COMPILE.cxx) {1}lt; -o $@

%.o:%.cc
$(COMPILE.cxx) {1}lt; -o $@

%.o:%.cpp
$(COMPILE.cxx) {1}lt; -o $@

%.o:%.CPP
$(COMPILE.cxx) {1}lt; -o $@

%.o:%.c++
$(COMPILE.cxx) {1}lt; -o $@

%.o:%.cp
$(COMPILE.cxx) {1}lt; -o $@

%.o:%.cxx
$(COMPILE.cxx) {1}lt; -o $@

# 生成tags的规则.
#-------------------------------------
tags: $(HEADERS) $(SOURCES)
$(ETAGS) $(ETAGSFLAGS) $(HEADERS) $(SOURCES)

ctags: $(HEADERS) $(SOURCES)
$(CTAGS) $(CTAGSFLAGS) $(HEADERS) $(SOURCES)

# 生成executable的规则.
#-------------------------------------
$(PROGRAM):$(OBJS)
ifeq ($(SRC_CXX),) # C program
$(LINK.c) $(OBJS) $(MY_LIBS) -o $@
@echo Type ./$@ to execute the program.
else # C++ program
$(LINK.cxx) $(OBJS) $(MY_LIBS) -o $@
@echo Type ./$@ to execute the program.
endif

# TANG MODIFY START
#ifndef NODEP
ifdef NODEP
# TANG MODIFY END
ifneq ($(DEPS),)
sinclude $(DEPS)
endif
endif

clean:
$(RM) $(OBJS) $(PROGRAM) $(PROGRAM).exe

distclean: clean
$(RM) $(DEPS) TAGS
#############################################################################

 

##############################################################################
# 显示帮助
help:
@echo ‘Generic Makefile for C/C++ Programs (gcmakefile) version 0.5‘
@echo ‘Copyright (C) 2007, 2008 whyglinux <whyglinux@hotmail.com>‘
@echo
@echo ‘Usage: make [TARGET]‘
@echo ‘TARGETS:‘
@echo ‘ all (=make) compile and link.‘
@echo ‘ NODEP=yes make without generating dependencies.‘
@echo ‘ objs compile only (no linking).‘
@echo ‘ tags create tags for Emacs editor.‘
@echo ‘ ctags create ctags for VI editor.‘
@echo ‘ clean clean objects and the executable file.‘
@echo ‘ distclean clean objects, the executable and dependencies.‘
@echo ‘ show show variables (for debug use only).‘
@echo ‘ help print this message.‘
@echo
@echo ‘Report bugs to <whyglinux AT gmail DOT com>.‘

# 显示变量 (仅用于调试用.)
show:
@echo ‘PROGRAM :‘ $(PROGRAM)
@echo ‘SRCDIRS :‘ $(SRCDIRS)
@echo ‘HEADERS :‘ $(HEADERS)
@echo ‘SOURCES :‘ $(SOURCES)
@echo ‘SRC_CXX :‘ $(SRC_CXX)
@echo ‘OBJS :‘ $(OBJS)
@echo ‘DEPS :‘ $(DEPS)
@echo ‘DEPEND :‘ $(DEPEND)
@echo ‘COMPILE.c :‘ $(COMPILE.c)
@echo ‘COMPILE.cxx :‘ $(COMPILE.cxx)
@echo ‘link.c :‘ $(LINK.c)
@echo ‘link.cxx :‘ $(LINK.cxx)
#############################################################################

linux Makefile(中文版1)

标签:

原文地址:http://www.cnblogs.com/Wanggcong/p/4786985.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!