一个通用的c/c++Makefile模版

一个codeproject上发现的通用c/c++的Makefile模版,比较简单好用,共享之


#############################################################################
#
# Generic Makefile for C/C++ Program
#
# License: GPL (General Public License)
# Author: whyglinux <whyglinux AT gmail DOT com>
# Date: 2006/03/04 (version 0.1)
# 2007/03/24 (version 0.2)
# 2007/04/09 (version 0.3)
# 2007/06/26 (version 0.4)
# 2008/04/05 (version 0.5)
#
# Description:
# ------------
# This is an easily customizable makefile template. The purpose is to
# provide an instant building environment for C/C++ programs.
#
# It searches all the C/C++ source files in the specified directories,
# makes dependencies, compiles and links to form an executable.
#
# Besides its default ability to build C/C++ programs which use only
# standard C/C++ libraries, you can customize the Makefile to build
# those using other libraries. Once done, without any changes you can
# then build programs using the same or less libraries, even if source
# files are renamed, added or removed. Therefore, it is particularly
# convenient to use it to build codes for experimental or study use.
#
# GNU make is expected to use the Makefile. Other versions of makes
# may or may not work.
#
# Usage:
# ------
# 1. Copy the Makefile to your program directory.
# 2. Customize in the "Customizable Section" only if necessary:
# * to use non-standard C/C++ libraries, set pre-processor or compiler
# options to <MY_CFLAGS> and linker ones to <MY_LIBS>
# (See Makefile.gtk+-2.0 for an example)
# * to search sources in more directories, set to <SRCDIRS>
# * to specify your favorite program name, set to <PROGRAM>
# 3. Type make to start building your program.
#
# 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.
##==========================================================================

# The pre-processor and compiler options.
MY_CFLAGS =

# The linker options.
MY_LIBS =

# The pre-processor options used by the cpp (man cpp for more).
CPPFLAGS = -Wall

# The options used in linking as well as in any direct use of ld.
LDFLAGS =

# The directories in which source files reside.
# If not specified, only the current directory will be serached.
SRCDIRS =

# The executable file name.
# If not specified, current directory name or `a.out‘ will be used.
PROGRAM =

## Implicit Section: change the following only when necessary.
##==========================================================================

# The source file types (headers excluded).
# .c indicates C source files, and others C++ ones.
SRCEXTS = .c .C .cc .cpp .CPP .c++ .cxx .cp

# The header file types.
HDREXTS = .h .H .hh .hpp .HPP .h++ .hxx .hp

# The pre-processor and compiler options.
# Users can override those variables from the command line.
CFLAGS = -g -O2
CXXFLAGS= -g -O2

# The C program compiler.
#CC = gcc

# The C++ program compiler.
#CXX = g++

# Un-comment the following line to compile C programs as C++ ones.
#CC = $(CXX)

# The command used to delete file.
#RM = rm -f

ETAGS = etags
ETAGSFLAGS =

CTAGS = ctags
CTAGSFLAGS =

## Stable Section: usually no need to be changed. But you can add more.
##==========================================================================
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)

## Define some useful variables.
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

# Delete the default suffixes
.SUFFIXES:

all: $(PROGRAM)

# Rules for creating dependency files (.d).
#------------------------------------------

%.d:%.c
@echo -n $(dir $<) > [email protected]
@$(DEPEND.d) $< >> [email protected]

%.d:%.C
@echo -n $(dir $<) > [email protected]
@$(DEPEND.d) $< >> [email protected]

%.d:%.cc
@echo -n $(dir $<) > [email protected]
@$(DEPEND.d) $< >> [email protected]

%.d:%.cpp
@echo -n $(dir $<) > [email protected]
@$(DEPEND.d) $< >> [email protected]

%.d:%.CPP
@echo -n $(dir $<) > [email protected]
@$(DEPEND.d) $< >> [email protected]

%.d:%.c++
@echo -n $(dir $<) > [email protected]
@$(DEPEND.d) $< >> [email protected]

%.d:%.cp
@echo -n $(dir $<) > [email protected]
@$(DEPEND.d) $< >> [email protected]

%.d:%.cxx
@echo -n $(dir $<) > [email protected]
@$(DEPEND.d) $< >> [email protected]

# Rules for generating object files (.o).
#----------------------------------------
objs:$(OBJS)

%.o:%.c
$(COMPILE.c) $< -o [email protected]

%.o:%.C
$(COMPILE.cxx) $< -o [email protected]

%.o:%.cc
$(COMPILE.cxx) $< -o [email protected]

%.o:%.cpp
$(COMPILE.cxx) $< -o [email protected]

%.o:%.CPP
$(COMPILE.cxx) $< -o [email protected]

%.o:%.c++
$(COMPILE.cxx) $< -o [email protected]

%.o:%.cp
$(COMPILE.cxx) $< -o [email protected]

%.o:%.cxx
$(COMPILE.cxx) $< -o [email protected]

# Rules for generating the tags.
#-------------------------------------
tags: $(HEADERS) $(SOURCES)
$(ETAGS) $(ETAGSFLAGS) $(HEADERS) $(SOURCES)

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

# Rules for generating the executable.
#-------------------------------------
$(PROGRAM):$(OBJS)
ifeq ($(SRC_CXX),) # C program
$(LINK.c) $(OBJS) $(MY_LIBS) -o [email protected]
@echo Type ./[email protected] to execute the program.
else # C++ program
$(LINK.cxx) $(OBJS) $(MY_LIBS) -o [email protected]
@echo Type ./[email protected] to execute the program.
endif

ifndef NODEP
ifneq ($(DEPS),)
sinclude $(DEPS)
endif
endif

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

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

# Show help.
help:
@echo ‘Generic Makefile for C/C++ Programs (gcmakefile) version 0.5‘
@echo ‘Copyright (C) 2007, 2008 whyglinux <[email protected]>‘
@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 variables (for debug use only.)
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)

## End of the Makefile ## Suggestions are welcome ## All rights reserved ##
#############################################################################

一个通用的c/c++Makefile模版,布布扣,bubuko.com

时间: 2025-01-14 08:15:24

一个通用的c/c++Makefile模版的相关文章

Linux C编程学习4---多文件项目管理、Makefile、一个通用的Makefile

GNU Make简介 大型项目的开发过程中,往往会划分出若干个功能模块,这样可以保证软件的易维护性. 作为项目的组成部分,各个模块不可避免的存在各种联系,如果其中某个模块发生改动,那么其他的模块需要相应的更新.如果通过手动去完成这个工作的话,对于小型的项目可能还行,但是对于比较大型的项目就几乎是不可能的. 因此Linux 系统提供了一个自动维护和生成目标程序的工具 make,它可以根据各个模块的更改情况去重新编译连接目标代码 Make 工具的作用就是实现编译连接过程的自动化.它定义了一种语言,用

一个通用Makefile的编写

作者:杨老师,华清远见嵌入式学院讲师. 我们在Linux环境下开发程序,少不了要自己编写Makefile,一个稍微大一些的工程下面都会包含很多.c的源文件.如果我们用gcc去一个一个编译每一个源文件的话,效率会低很多,但是如果我们可以写一个Makefile,那么只需要执行一个make就OK了,这样大大提高了开发效率.但是Makefile的语法规则众多,而且缺乏参考资料,对于初学者来说,写起来还是有一定的难度,往往令很多人望而生畏.下面我们介绍一个比较通用而且简洁的Makefile,大家只要对它稍

一个通用的Makefile (转)

据http://bbs.chinaunix.net/thread-2300778-1-1.html的讨论,发现还是有很多人在问通用Makefile的问题,这里做一个总结.也作为以后的参考. 笔者在写程序的时候会遇到这样的烦恼:一个项目中可能会有很多个应用程序,而新建一个应用程序则所有的Makefile都要重写一遍,虽然可以部分的粘帖复制,但还是感觉应该找到更好的解决途径:另外当一个应用程序中包含多个文件夹时通常要在每个目录下创建一个Makefile,当有数十个文件夹时,要创建如此多的Makefi

一个通用Makefile详解

我们在Linux环境下开发程序,少不了要自己编写Makefile,一个稍微大一些的工程下面都会包含很多.c的源文 件. 如果我们用gcc去一个一个编译每一个源文件的话,效率会低很多,但是如果我们可以写一个Makefile,那么只需要执行一个make就OK了,这 样大大提高了开发效率. 但是Makefile的语法规则众多,而且缺乏参考资料,对于初学者来说,写起来还是有一定的难度,往往令很多人望而生畏. 下面我 们介绍一个比较通用而且简洁的Makefile,大家只要对它稍作修改就可以用在你们自己的工

编写一个通用的Makefile文件

1.1在这之前,我们需要了解程序的编译过程 a.预处理:检查语法错误,展开宏,包含头文件等 b.编译:*.c-->*.S c.汇编:*.S-->*.o d.链接:.o +库文件=*.exe 1.2体验在VC下程序的编译 a.先编译,在链接 b.修改了哪个文件,就单独编译此文件,在链接 c.修改了哪个头文件,就单独编译使用该头文件的源文件,在链接 1.3在linux下实现上述要求 2.编写一个测试的Makefile 2.1直接编译链接 1 gcc -o test a.c b.c 缺点:改变其中一

移动应用开发(IOS/android等)中一个通用的图片缓存方案讲解(附流程图)

在移动应用开发中,我们经常会遇到从网络请求图片到设备上展示的场景. 如果每次都重复发起请求,浪费流量.浪费电量,用户体验也不佳: 将图片持久化到磁盘也不失为一种策略:但每次从文件读取图片也存在一定的io开销,就算采用此策略,我们也需要控制磁盘缓存的容量,以免占用过多系统资源. 其实没有一个方案可以说是完美的方案,只有最适合自己业务需求的方案,才可以说是一个好方案. 我们下面所讲解的方案具备很强的通用性,设计思路简单而清晰: 1.假设每个网络图片的url具有唯一性,如果网络上的图片变化了,会引起输

一个通用的JavaScript分页

1.JavaScript代码 Java代码   Pagination=function(id) { var totalNum=0; var maxNum=10; var pageUrl=""; var breakpage = 5; var currentposition = 0; var breakspace = 2; var maxspace = 4; var currentpage=1; var perpage=10; var id =id; this.initPage = fun

为了去重复,写了一个通用的比较容器类,可以用在需要比较的地方,且支持Lamda表达式

为了去重复,写了一个通用的比较容器类,可以用在需要比较的地方,且支持Lamda表达式,代码如下: public class DataComparer<T>:IEqualityComparer<T> where T:class { private Func<T, T, bool> _compareFunc; public DataComparer(Func<T,T,bool> compareFunc) { this._compareFunc = compare

分享一个通用的分页SQL

又很久没写博客,今天记录一个SQLserver通用分页存储过程(适用于SqlServer2000及以上版本) 1.支持连表 2.支持条件查询 USE [MYDB] GO /****** Object:  StoredProcedure [dbo].[SP_CommonPage] SET QUOTED_IDENTIFIER ON GO ------------------------------------ --用途:分页存储过程(对有主键的表效率极高) --说明: ---------------