makefile
示例Makefile
#rule.make
CC=g++ MAKE=make AR=ar cr RM=rm -rf #CFLAGS+=-Wall OBJDIR=.out SRCSCPP=$(wildcard *.cpp) SRCSC=$(wildcard *.c) OBJS=$(SRCSCPP:%.cpp=$(OBJDIR)/%.o) $(SRCSC:%.c=$(OBJDIR)/%.o) DEPENDS=$(SRCSCPP:%.cpp=$(OBJDIR)/%.d) $(SRCSC:%.c=$(OBJDIR)/%.d) .PONEY: clean all all:$(TARGET) $(LIB) subdirs $(DEPENDS) $(LIB):$(OBJS) @mkdir -p $(LIBPATH) @$(AR) $(LIBPATH)/$@ $^ @echo create $@ $(TARGET):$(OBJS) @mkdir -p $(EXEPATH) @$(CC) -o $(EXEPATH)/$@ $^ $(LDFLAGS) @echo create $@ subdirs:$(SUBDIRS) @for dir in $(SUBDIRS);\ do $(MAKE) -C $$dir all||exit 1;\ done $(OBJDIR)/%.o:%.cpp $(OBJDIR)/%.d @mkdir -p $(OBJDIR) @$(CC) -c $< -o $@ $(CFLAGS) @echo "Compile $<" $(OBJDIR)/%.o:%.c $(OBJDIR)/%.d @mkdir -p $(OBJDIR) @$(CC) -c $< -o $@ $(CFLAGS) @echo "Compile $<" #-include $(DEPENDS) $(OBJDIR)/%.d:%.c @mkdir -p $(OBJDIR) @set -e; rm -f $@;\ $(CC) -MM $(CFLAGS) $< > $@.$$$$;\ sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@;\ rm -f $@.$$$$ $(OBJDIR)/%.d:%.cpp @mkdir -p $(OBJDIR) @set -e; rm -f $@;\ $(CC) -MM $(CFLAGS) $< > $@.$$$$;\ sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@;\ rm -f $@.$$$$ clean: @for dir in $(SUBDIRS);\ do \ echo "clean dir $$dir";\ $(MAKE) -C $$dir clean||exit 1;\ done @-$(RM) $(TARGET) $(LIB) $(OBJDIR)
#Makefile lib
TOPDIR=.. LIB=libsourcecmd.a LIBPATH=$(TOPDIR)/.lib/ CFLAGS= -I./\ -I../../comm/libs/utils\ -I../../comm/libs3/mysql/include\ -I../../comm/libs3\ -I../../comm/libs3/libuv-1.x/include include $(TOPDIR)/rules.make
#Makefile exe
TOPDIR=.. TARGET=cmu EXEPATH=$(TOPDIR)/bin LIBPATH=-L$(TOPDIR)/.lib\ -L../../comm/libs3/libuv-1.x/.libs\ -L../../comm/share/.lib\ -L../../comm/libs/asio/.lib\ -L../../comm/libs/db/.lib\ -L/usr/lib64/mysql CFLAGS= -I./\ -I../../comm/libs/utils\ -I../../comm/libs3/mysql/include\ -I../../comm/libs3\ -I../../comm/libs3/libuv-1.x/include LDFLAGS= $(LIBPATH) -lsourcecmd -lsourcedb -ldb -lasio -lshare -lm -ldl -lpthread -luv -lmysqlclient include $(TOPDIR)/rules.make
说明:
include $(DEPENDS)包含头文件依赖文件很多事是会导致反应慢和两次进入子目录问题
一个直接编译多个目录下文件的makefile
CC=g++ MAKE=make AR=ar cr RM=rm -rf #CFLAGS+=-Wall LIBPATH=-L../comm/libs3/libuv-1.x/.libs\ -L../comm/share/.lib\ -L../comm/libs/asio/.lib\ -L../comm/libs/db/.lib\ -L/usr/lib64/mysql CFLAGS= -I./\ -I../comm/libs/utils\ -I../comm/libs3/mysql/include\ -I../comm/libs3\ -I../comm/libs3/libuv-1.x/include LDFLAGS= $(LIBPATH) -lm -ldl -lpthread -luv -lmysqlclient SRCSCPP=$(wildcard ../comm/libs/asio/*.cpp)\ $(wildcard ../comm/libs/db/*.cpp)\ $(wildcard ../comm/share/*.cpp)\ $(wildcard sourcecmd/*.cpp)\ $(wildcard sourcedb/*.cpp)\ $(wildcard source/*.cpp) OBJS=$(SRCSCPP:%.cpp=%.o) TARGET=test all:$(TARGET) $(TARGET):$(OBJS) @$(CC) -o $(EXEPATH)/$@ $^ $(LDFLAGS) @echo create $@ %.o:%.cpp @$(CC) -c $< -o $@ $(CFLAGS) @echo "Compile $<" clean: @-$(RM) $(TARGET) $(OBJS)
头文件依赖关系另外的写法:
%.d:%.cpp $(GCC) -MM -MT "$*.o $*.d" $(INCS) $< >$@
文件排除
SRC := $(filter-out a.cpp b.cpp, $(SRC))
Make
will set the special variable MAKECMDGOALS
to the list of goals you specified on the command line. If no goals were given on the command line, this variable is empty. Note that this variable should be used only in special circumstances.
参考:
头文件依赖关系
https://blog.csdn.net/dragon101788/article/details/30580349
层级makefile