新年福利,个人项目Makefile模板

在linux下编译一个项目代码少不了Makefile文件,但是每次都要重新写一个Makefile总是会让人很头疼

C/C++项目都可以用的Makefile

TYPE=release CC=gcc CFLAGS=-fPIC -Wall ifeq ($(TYPE),debug) CFLAGS+=-g3 -pg -DDEBUG else

    ifeq ($(TYPE),release)
        CFLAGS
+=-O3 else

        CFLAGS
+=-g -O endif endif CXX=g++

CXXFLAGS
=-fPIC -Wall ifeq ($(TYPE),debug) CXXFLAGS+=-g3 -pg -DDEBUG else

    ifeq ($(TYPE),release)
        CXXFLAGS
+=-O3 else

        CXXFLAGS
+=-g -O endif endif LD=g++

LDFLAGS
=


AR
=ar ARFLAGS=rc RM=rm MV=mv ############Parameter Setting Start########### #sub directory && exe directory, take care the order! The one depended by other one must be put ahead. #MODDIRS=GrabCut ImageROI extractROI Mpeg7_Feature LocalFeatures ml VisualWords FaceDetect MODDIRS=


OUTPUT_MODULE
=


SUBDIRS
=$(MODDIRS) $(OUTPUT_MODULE) #source files directory, default is current directory #be sure that your source files is under this directory SRC_DIR=./extractROI ./GrabCut ./GrabCut/maxflow/adjacency_list ./ImageROI ./LocalFeatures ./LocalFeatures/Asift ./Mpeg7_Feature/ ./Mpeg7_Feature/AddressLib ./Mpeg7_Feature/Descriptors ./Mpeg7_Feature/Extraction ./ml ./VisualWords ./FaceDetect #included header files directory, you can specify more than one directory #names here seperated by whitespace, e.g. INCDIR=-I. -I../inc1 -I../inc2 #don't forget add '-I' INCDIR=-I../include -I../include/opencv -I../include/extractroi -I../include/imageroi -I../include/grabcut -I../include/grabcut/maxflow -I../include/localfeatures -I../include/localfeatures/asift -I./LocalFeatures/Asift -I../include/mpeg7_feature -I../include/mpeg7_feature/AddressLib -I../include/mpeg7_feature/Descriptors -I../include/mpeg7_feature/Extraction -I../include/facedetect -I../include/ml -I../include/visualwords #vpath %.h #specify the path of the library if needed #don't forget add '-L' or '-l{libname}' LIBS=


#binary files name and path
MODULE_NAME
=


#static
library target STATICLIB=../lib/libSeeBasic.a #dynamic library target SHARELIB=../lib/libSeeBasic.so SRCS:=$(wildcard $(addsuffix /*
.c,$(SRC_DIR)))\
      $(wildcard $(addsuffix /*.C,$(SRC_DIR)))\
      $(wildcard $(addsuffix /*.cpp,$(SRC_DIR)))\
      $(wildcard $(addsuffix /*.CPP,$(SRC_DIR)))

OBJS:=$(SRCS:%.c=%.o)
OBJS:=$(OBJS:%.C=%.o)
OBJS:=$(OBJS:%.cpp=%.o)
OBJS:=$(OBJS:%.CPP=%.o)

DEPS:=$(OBJS:%.o=%.d)

#if you want to only compile the source files(don't need to link the program), write like this 'COMPILE=$(OBJS)', and let the other TARGET be empty.
COMPILE=

############Parameter Setting End###########

.PHONY:

TARGET=$(MODULE_NAME) $(STATICLIB) $(SHARELIB)

all:$(COMPILE)
    @set -e;for dir in ${SUBDIRS};        \
          do                          \
            ${MAKE} all -C $${dir};   \
    done
    @set -e;for target in ${TARGET};      \
          do                          \
            ${MAKE} $${target};       \
    done

$(MODULE_NAME):$(OBJS)
    $(LD) $^ $(LDFLAGS) $(LIBS) -o $@
    @echo "  ^_^ LD $@"

static-lib:$(STATICLIB)

$(STATICLIB):$(OBJS)
    $(AR) $^ $(ARFLAGS) $@
    @echo "  ^_^ AR $@"

share-lib:$(SHARELIB)

$(SHARELIB):$(OBJS)
    $(LD) $^ -fPIC -shared $(LDFLAGS) $(LIBS) -o $@
    @echo "  ^_^ LD $@"

debug:
    $(MAKE) TYPE=debug

clean:
    @set -e;for dir in ${SUBDIRS};        \
          do                          \
                ${MAKE} clean -C $${dir}; \
    done
    @$(RM) -rf $(OBJS)
    @$(RM) -rf $(STATICLIB)
    @$(RM) -rf $(SHARELIB)
    @$(RM) -rf $(DEPS)
    @$(RM) -rf *~
    @echo "  ^_^ RM ALL"

.SUFFIXES: .o .c .C .cpp .CPP

-include $(DEPS)

%.o:%.c
    if $(CC) $(INCDIR) $(CFLAGS) -MT $@ -MD -MP -MF "$(@D)/$(@F:%.o=%.td)" -c -o $@ $<; then \
           $(MV) -f "$(@D)/$(@F:%.o=%.td)" "$(@D)/$(@F:%.o=%.d)"; \
    else \
       $(RM) -f "$(@D)/$(@F:%.o=%.td)"; exit 1; \
    fi
    @echo "  ^_^ CC $@"
       
%.o:%.C
    if $(CC) $(INCDIR) $(CFLAGS) -MT $@ -MD -MP -MF "$(@D)/$(@F:%.o=%.td)" -c -o $@ $<; then \
           $(MV) -f "$(@D)/$(@F:%.o=%.td)" "$(@D)/$(@F:%.o=%.d)"; \
    else \
       $(RM) -f "$(@D)/$(@F:%.o=%.td)"; exit 1; \
    fi
    @echo "  ^_^ CC $@"

%.o:%.cpp
    if $(CXX) $(INCDIR) $(CXXFLAGS) -MT $@ -MD -MP -MF "$(@D)/$(@F:%.o=%.td)" -c -o $@ $<; then \
           $(MV) -f "$(@D)/$(@F:%.o=%.td)" "$(@D)/$(@F:%.o=%.d)"; \
    else \
       $(RM) -f "$(@D)/$(@F:%.o=%.td)"; exit 1; \
    fi
    @echo "  ^_^ CXX $@"

%.o:%.CPP
    if $(CXX) $(INCDIR) $(CXXFLAGS) -MT $@ -MD -MP -MF "$(@D)/$(@F:%.o=%.td)" -c -o $@ $<; then \
           $(MV) -f "$(@D)/$(@F:%.o=%.td)" "$(@D)/$(@F:%.o=%.d)"; \
    else \
       $(RM) -f "$(@D)/$(@F:%.o=%.td)"; exit 1; \
    fi
    @echo "  ^_^ CXX $@"
原文地址:https://www.cnblogs.com/lvfq/p/2841358.html