Makefile的编写方法
#############################################################################
# Makefile
#############################################################################
####### Compiler, tools and options
CXX = g++
CPPFLAGS = -pipe -Wall -W -pthread -g -D_REENTRANT
INCPATH = .
LIBS =
EXECUTABLE =
RM = rm -rf
SRCS = $(wildcard *.cc)
OBJS = $(patsubst %.cc,%.o,$(SRCS))
DEPS = $(patsubst %.o,%.d,$(OBJS))
CPPFLAGS += -MMD $(addprefix -I, $(INCPATH))
MISSING_DEPS = $(filter-out $(wildcard $(DEPS)),$(DEPS))
MISSING_DEPS_SRCS = $(wildcard $(patsubst %.d,%.cc,$(MISSING_DEPS)))
.PHONY : everything deps objs clean veryclean rebuild
everything : $(EXECUTABLE)
deps : $(DEPS)
objs : $(OBJS)
clean :
@$(RM) $(OBJS)
@$(RM) $(DEPS)
veryclean: clean
@$(RM) $(EXECUTABLE)
rebuild: veryclean everything
ifneq ($(MISSING_DEPS),)
$(MISSING_DEPS):
@$(RM) $(patsubst %.d,%.o,$@)
endif
-include $(DEPS)
$(EXECUTABLE) : $(OBJS)
$(CXX) -o $(EXECUTABLE) $(OBJS) $(addprefix -l,$(LIBS))
# Makefile
#############################################################################
####### Compiler, tools and options
CXX = g++
CPPFLAGS = -pipe -Wall -W -pthread -g -D_REENTRANT
INCPATH = .
LIBS =
EXECUTABLE =
RM = rm -rf
SRCS = $(wildcard *.cc)
OBJS = $(patsubst %.cc,%.o,$(SRCS))
DEPS = $(patsubst %.o,%.d,$(OBJS))
CPPFLAGS += -MMD $(addprefix -I, $(INCPATH))
MISSING_DEPS = $(filter-out $(wildcard $(DEPS)),$(DEPS))
MISSING_DEPS_SRCS = $(wildcard $(patsubst %.d,%.cc,$(MISSING_DEPS)))
.PHONY : everything deps objs clean veryclean rebuild
everything : $(EXECUTABLE)
deps : $(DEPS)
objs : $(OBJS)
clean :
@$(RM) $(OBJS)
@$(RM) $(DEPS)
veryclean: clean
@$(RM) $(EXECUTABLE)
rebuild: veryclean everything
ifneq ($(MISSING_DEPS),)
$(MISSING_DEPS):
@$(RM) $(patsubst %.d,%.o,$@)
endif
-include $(DEPS)
$(EXECUTABLE) : $(OBJS)
$(CXX) -o $(EXECUTABLE) $(OBJS) $(addprefix -l,$(LIBS))
说明: 1,CPPFLAGS里面的-MMD是针对gnu的compilers的,跟-MD(其他compilers)的区别是-MMD去掉了depends里面的系统的头文件(使用#include 的头文件) 2,@$(RM) $(OBJS),这里的@是让编译的时候不显示这条命令,gnu make默认是显示命令的 3,-include $(DEPS),前面的-号是为了忽略当后面的文件不存在的errors 4,.PHONY : everything deps objs clean veryclean rebuild,这句是为了让make不检测:后面的项是否存在同名的文件 |
|