示例Makefilehtml
#rule.makemysql
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 libsql
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 exethis
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
說明:spa
include $(DEPENDS)包含頭文件依賴文件不少事是會致使反應慢和兩次進入子目錄問題.net
一個直接編譯多個目錄下文件的makefilecode
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)
頭文件依賴關係另外的寫法:htm
%.d:%.cpp $(GCC) -MM -MT "$*.o $*.d" $(INCS) $< >$@
文件排除blog
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.ci
參考:
頭文件依賴關係
https://blog.csdn.net/dragon101788/article/details/30580349
層級makefile