模板1
#--------------------------------------------------
#======== A simple Makefile template--lchh ===========
CC=gcc
XX=g++
CFLAGS = -Wall -O -g
TARGET = ./main
%.o : %.c
$(CC) $(CFLAGS) -c $< -o $@
%.o : %.cpp
$(XX) $(CFLAGS) -c $< -o $@
SOURCES = $(wildcard *.c *.cpp)
OBJS = $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCES)))
$(TARGET) : $(OBJS)
$(XX) $(OBJS) -o $(TARGET)
rm -rf *.o
chmod a+x $(TARGET)
clean:
rm -rf *.o $(TARGET)
#--------------------------------------------------
模板2
#--------------------------------------------------
# 下面的变量可以在shell 的环境变量里面指定。
# 也可以象下面这样在 Makefile 里面指定。
# CC=gcc # 编译器
# CFLAGS=-Wall -Werror -g # 编译器参数
# LD=gcc # 连接器参数
# LDFLAGS= $(LIBS) -lpthread # 连接器参数
# DEPENDFLAG=-MM # 生成依赖关系文件的参数
# INCLUDES=-Idir1 -Idir2 # 指明包含外部头文件的目录
# LIBS=-la -lb -lc # 指明引用外部的库文件
CFLAGS:=$(CFLAGS) $(INCLUDES)
LDFLAGS:=$(LDFLAGS) $(LIBS)
#指明项目中,包含源程序的所有的子目录。
SRCDIRS=.
#指明最终生成的可执行文件的名称
PROGRAMS=test.exe
#下面的部分一般不用改动
#从所有子目录中得到源代码的列表
SRCS=$(foreach dir,$(SRCDIRS),$(wildcard $(dir)/*.c))
#得到源代码对应的目标文件的列表
OBJS=$(SRCS:.c=.o)
#得到源代码对应的依赖关系文件的列表
#依赖关系文件就是一个目标文件依赖于
#哪些头文件和源程序,依赖关系是自动
#生成的,并且用include语句包含在Makefile中
DEPENDS=$(SRCS:.c=.d)
#指明默认目标是生成最终可执行文件。
all: $(PROGRAM)
#生成依赖关系文件
%.d:%.c
$(CC) $(DEPENDFLAG) $(CFLAGS) $< |/
sed "s?//(.*//):?$(basename $<).o $(basename $<).d :?g" /
> $@ || $(RM) $@
$(PROGRAMS): $(OBJS)
$(CC) $(LDFLAGS) -o $@ $(filter %.o ,$+)
# 包含入依赖关系文件
include $(DEPENDS)
# 删除生成的中间文件
clean:
rm $(OBJS) $(DEPENDS) $(PROGRAMS)
#--------------------------------------------------
模板3
#--------------------------------------------------
CC=gcc
CFLAGS=-Wall
OBJPATH=obj # .d文件和.o文件保存路径
TARGET=demo # 最终可执行文件
default: $(TARGET)
SOURCE=a.c b.c # 需要编译的.c文件
INCLUDE=
# 获取.d文件名集合
DEPS=$(addprefix $(OBJPATH)/, $(patsubst %.c, %.d, $(filter %.c, $(SOURCE))))
# 获取.o文件名集合
OBJS=$(addprefix $(OBJPATH)/, $(addsuffix .o, $(basename $(SOURCE))))
-include $(DEPS)
# 生成.d文件
$(OBJPATH)/%.d: %.c
$(CC) $(INCLUDE) -MM $< | sed "1s|^|$(dir $@)|" |"
sed "1{x;s|.*|$@: $<|;G;}" > $(basename $@).d
# 生成.o文件
$(OBJPATH)/%.o : %.c
$(CC) $(CFLAGS) -o $@ -c $<
# 链接成可执行文件
$(TARGET): $(OBJS)
$(CC) $(CFLAGS) -o $@ $^
# 清理
clean:
rm -rf $(OBJPATH)/*.d $(OBJPATH)/*.o $(TARGET)
#--------------------------------------------------
模板4
#--------------------------------------------------
#=======================================================================================
#
# Filename: Makefile
# Description:
#
# Usage: make (generate executable )
# make clean (remove objects, executable, prerequisits )
# make tarball (generate compressed archive )
# make zip (generate compressed archive )
#
# Version: 1.0
# Created:
# Revision: ---
#
# Author:
# Company:
# Email:
#
# Notes: C extension : c
# C++ extensions : cc cpp C
# C and C++ sources can be mixed.
# Prerequisites are generated automatically; makedepend is not
# needed (see documentation for GNU make Version 3.80, July 2002,
# section 4.13). The utility sed is used.
#
#============================================== makefile template version 1.6 ==========
# ------------ name of the executable ------------------------------------------------
EXECUTABLE = main
# ------------ list of all source files ----------------------------------------------
SOURCES = main.c
# ------------ compiler --------------------------------------------------------------
CC = gcc
CXX = g++
# ------------ compiler flags --------------------------------------------------------
CFLAGS = -Wall -O0 -g # Do not optimize. Produce debugging information.
# ------------ linker-Flags ----------------------------------------------------------
LFLAGS = -g
# ------------ additional system include directories ---------------------------------
GLOBAL_INC_DIR =
# ------------ private include directories -------------------------------------------
LOCAL_INC_DIR = $(HOME)/include
# ------------ system libraries (e.g. -lm ) -----------------------------------------
SYS_LIBS = -lm
# ------------ additional system library directories ---------------------------------
GLOBAL_LIB_DIR =
# ------------ additional system libraries -------------------------------------------
GLOBAL_LIBS =
# ------------ private library directories -------------------------------------------
LOCAL_LIB_DIR = $(HOME)/lib
# ------------ private libraries (e.g. libxyz.a ) -----------------------------------
LOCAL_LIBS =
# ------------ archive generation -----------------------------------------------------
TARBALL_EXCLUDE = *.{o,gz,zip}
ZIP_EXCLUDE = *.{o,gz,zip}
# ------------ run executable out of this Makefile (yes/no) -------------------------
# ------------ cmd line parameters for this executable -------------------------------
EXE_START = no
EXE_CMDLINE =
#=======================================================================================
# The following statements usually need not to be changed
#=======================================================================================
C_SOURCES = $(filter %.c, $(SOURCES))
CPP_SOURCES = $(filter-out %.c, $(SOURCES))
ALL_INC_DIR = $(addprefix -I, $(LOCAL_INC_DIR) $(GLOBAL_INC_DIR))
ALL_LIB_DIR = $(addprefix -L, $(LOCAL_LIB_DIR) $(GLOBAL_LIB_DIR))
GLOBAL_LIBSS = $(addprefix $(GLOBAL_LIB_DIR)/, $(GLOBAL_LIBS))
LOCAL_LIBSS = $(addprefix $(LOCAL_LIB_DIR)/, $(LOCAL_LIBS))
ALL_CFLAGS = $(CFLAGS) $(ALL_INC_DIR)
ALL_LFLAGS = $(LFLAGS) $(ALL_LIB_DIR)
BASENAMES = $(basename $(SOURCES))
# ------------ generate the names of the object files --------------------------------
OBJECTS = $(addsuffix .o,$(BASENAMES))
# ------------ generate the names of the hidden prerequisite files -------------------
PREREQUISITES = $(addprefix .,$(addsuffix .d,$(BASENAMES)))
# ------------ make the executable ---------------------------------------------------
$(EXECUTABLE): $(OBJECTS)
ifeq ($(strip $(CPP_SOURCES)),)
$(CC) $(ALL_LFLAGS) -o $(EXECUTABLE) $(OBJECTS) $(LOCAL_LIBSS) $(GLOBAL_LIBSS) $(SYS_LIBS)
else
$(CXX) $(ALL_LFLAGS) -o $(EXECUTABLE) $(OBJECTS) $(LOCAL_LIBSS) $(GLOBAL_LIBSS) $(SYS_LIBS)
endif
ifeq ($(EXE_START),yes)
./$(EXECUTABLE) $(EXE_CMDLINE)
endif
# ------------ include the automatically generated prerequisites ---------------------
# ------------ if target is not clean, tarball or zip ---------------------
ifneq ($(MAKECMDGOALS),clean)
ifneq ($(MAKECMDGOALS),tarball)
ifneq ($(MAKECMDGOALS),zip)
include $(PREREQUISITES)
endif
endif
endif
# ------------ make the objects ------------------------------------------------------
%.o: %.c
$(CC) -c $(ALL_CFLAGS) $<
%.o: %.cc
$(CXX) -c $(ALL_CFLAGS) $<
%.o: %.cpp
$(CXX) -c $(ALL_CFLAGS) $<
%.o: %.C
$(CXX) -c $(ALL_CFLAGS) $<
# ------------ make the prerequisites ------------------------------------------------
#
.%.d: %.c
@$(make-prerequisite-c)
.%.d: %.cc
@$(make-prerequisite-cplusplus)
.%.d: %.cpp
@$(make-prerequisite-cplusplus)
.%.d: %.C
@$(make-prerequisite-cplusplus)
# canned command sequences
# echoing of the sed command is suppressed by the leading @
define make-prerequisite-c
@$(CC) -MM $(ALL_CFLAGS) $< > $@.$$$$; /
sed 's//($*/)/.o[ :]*//1.o $@ : /g' < $@.$$$$ > $@; /
rm -f $@.$$$$;
endef
define make-prerequisite-cplusplus
@$(CXX) -MM $(ALL_CFLAGS) $< > $@.$$$$; /
sed 's//($*/)/.o[ :]*//1.o $@ : /g' < $@.$$$$ > $@; /
rm -f $@.$$$$;
endef
# ------------ remove generated files ------------------------------------------------
# ------------ remove hidden backup files --------------------------------------------
clean:
rm --force $(EXECUTABLE) $(OBJECTS) $(PREREQUISITES) *~
# ------------ tarball generation ------------------------------------------------------
tarball:
@lokaldir=`pwd`; lokaldir=$${lokaldir##*/}; /
rm --force $$lokaldir.tar.gz; /
tar --exclude=$(TARBALL_EXCLUDE) /
--create /
--gzip /
--verbose /
--file $$lokaldir.tar.gz *
# ------------ zip ---------------------------------------------------------------------
zip:
@lokaldir=`pwd`; lokaldir=$${lokaldir##*/}; /
zip -r $$lokaldir.zip * -x $(ZIP_EXCLUDE)
# ======================================================================================
# vim: set tabstop=2: set shiftwidth=2:
#--------------------------------------------------