C, C++混合編程通用Makefile模版__Linux

來源:互聯網
上載者:User

第一個模版:所有.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
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.