第一個模版:所有.c, .cpp, *.h檔案都在同一個目錄下,沒有分層。
##############################################################################source fileSOURCE := $(wildcard *.c) $(wildcard *.cpp)OBJS := $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCE)))#target you can change test to what you wantTARGET := host_send #compile and lib parameterCC := gccCXX := g++LIBS := -lusb-1.0 -luvc -lopencv_highgui -lreadline -lopencv_imgcodecs -lopencv_imgproc -lc -lopencv_calib3d -lopencv_core#LIBS := -lusb-1.0 -luvcLDFLAGS := -L/usr/libx86_64-linux-gnu/ -L/usr/lib/x86_64-linux-gnu -L/usr/local/lib -L.DEFINES := -fpic -Wl,-rpath=.INCLUDE := -I. -I./include -I/usr/include/libusb-1.0 -I/usr/local/includeCFLAGS := -g -Wall -O3 $(DEFINES) $(INCLUDE)CXXFLAGS:= $(CFLAGS)#i think you should do anything here.PHONY : everything objs clean veryclean rebuildeverything : $(TARGET)all : $(TARGET)objs : $(OBJS)rebuild: veryclean everythingclean : rm -rf *.o rm $(TARGET)veryclean : clean rm -rf $(TARGET)$(TARGET) : $(OBJS) $(CXX) $(CXXFLAGS) -o $@ $(OBJS) $(LDFLAGS) $(LIBS)
第二個模版:.c和.cpp檔案在src目錄,檔案結構更清晰。
BINFILE = testifdef VERBOSE Q = E = @true else Q = @ E = @echo endifCFILES := $(shell find src -mindepth 1 -maxdepth 4 -name "*.c")CXXFILES := $(shell find src -mindepth 1 -maxdepth 4 -name "*.cpp")INFILES := $(CFILES) $(CXXFILES)OBJFILES := $(CXXFILES:src/%.cpp=%) $(CFILES:src/%.c=%)DEPFILES := $(CXXFILES:src/%.cpp=%) $(CFILES:src/%.c=%)OFILES := $(OBJFILES:%=obj/%.o)COMMONFLAGS := -Wall -Wextra -pedantic -O3 DEFINES := -fpic -Wl,-rpath=.INCLUDE := -I. -I./include -I/usr/include/libusb-1.0 -I/usr/local/includeLDFLAGS := -L/usr/libx86_64-linux-gnu/ -L/usr/lib/x86_64-linux-gnu -L/usr/local/lib -L.LIBS := -lusb-1.0 -lopencv_core -lopencv_highgui -lreadline -lopencv_calib3d -lopencv_imgcodecs -lc -lopencv_imgprocifdef DEBUG COMMONFLAGS := $(COMMONFLAGS) -gendifCFLAGS := $(COMMONFLAGS) --std=c99 $(DEFINES) $(INCLUDE)CXXFLAGS := $(COMMONFLAGS) --std=c++0x $(DEFINES) $(INCLUDE)DEPDIR := depsall: $(BINFILE)ifeq ($(MAKECMDGOALS),)-include Makefile.dependififneq ($(filter-out clean, $(MAKECMDGOALS)),)-include Makefile.dependifCC = gccCXX = g++-include Makefile.local.PHONY: clean all depend.SUFFIXES:obj/%.o: src/%.c $(E)C-compiling $< $(Q)if [ ! -d `dirname $@` ]; then mkdir -p `dirname $@`; fi $(Q)$(CC) -o $@ -c $< $(CFLAGS)obj/%.o: src/%.cpp $(E)C++-compiling $< $(Q)if [ ! -d `dirname $@` ]; then mkdir -p `dirname $@`; fi $(Q)$(CXX) -o $@ -c $< $(CXXFLAGS)Makefile.dep: $(CFILES) $(CXXFILES) $(E)Depend $(Q)for i in $(^); do $(CXX) $(CXXFLAGS) -MM "$${i}" -MT obj/`basename $${i%.*}`.o; done > $@$(BINFILE): $(OFILES) $(E)Linking $@ $(Q)$(CXX) -o $@ $(OFILES) $(LDFLAGS) $(LIBS)clean: $(E)Removing files $(Q)rm -f $(BINFILE) obj/* Makefile.dep