几个Makefile模板

    2021/11/19 14:55 下午 标签: #编程

    模板1

    #
    # c.cpp混合编译的makefile模板
    #
    #
    
    
    BIN = 
    CROSS = 
    CC = $(CROSS)gcc
    CPP = $(CROSS)g++
    LD = $(CROSS)ld
    #这里只加入库头文件路径及库路径
    INCS = #-I"c:/mingw/include"
    LIBS = #-L"c:/mingw/lib" 
    SUBDIRS =
    #生成依赖信息时的搜索目录,比如到下列目录中搜索一个依赖文件(比如.h文件),例如 -I"./***/" -I"./base/"
    DEFINC = -I./snap7/ -I./snap7/src/sys/ -I./snap7/src/lib/ -I./snap7/src/core/
    #给INCS加上依赖搜索路径,分开写可能会产生不一致情况,而且繁琐
    INCS += $(DEFINC)
    #
    #maintest.c tree/rbtree.c  多了子目录,那就直接添加 目录/*.c即可   所有的源文件--  .c文件列表
    CSRCS = $(wildcard ./*.c ./snap7/*.c)
    CPPSRCS = $(wildcard ./snap7/src/sys/*.cpp ./snap7/src/lib/*.cpp ./snap7/src/core/*.cpp)
    #
    #所有的.o文件列表
    COBJS := $(CSRCS:.c=.o)
    CPPOBJS := $(CPPSRCS:.cpp=.o)
    #
    #生成依赖信息 -MM是只生成自己的头文件信息,-M 包含了标准库头文件信息。
    #-MT 或 -MQ都可以改变生成的依赖  xxx.o:src/xxx.h 为 src/xxx.o:src/xxx.h 当然。前面的 src/xxx.o需自己指定
    #格式为 -MM 输入.c或.cpp  查找依赖路径  -MT或-MQ  生成规则,比如src/xxx.o
    #MAKEDEPEND = gcc -MM -MT
    CFLAGS += $(INCS)
    CFLAGS += -O2 -Wall -g -fPIC
    CPPFLAGS += $(INCS)
    CPPFLAGS += -O2 -Wall -g -fPIC
    LDFLAGS += -shared -fPIC -lpthread -lm -lrt -lstdc++
    
    all:$(BIN)
    #$(OBJS):%.o :%.c  先用$(OBJS)中的一项,比如foo.o: %.o : %.c  含义为:试着用%.o匹配foo.o。如果成功%就等于foo。如果不成功,
    # Make就会警告,然后。给foo.o添加依赖文件foo.c(用foo替换了%.c里的%)
    # 也可以不要下面的这个生成规则,因为下面的 include $(DEF)  就隐含了。此处为了明了,易懂。故留着
    $(COBJS) : %.o: %.c
    	$(CC) $(CFLAGS) -c $< -o $@ 
    $(CPPOBJS) : %.o: %.cpp
    	$(CPP) $(CPPFLAGS) -c $< -o $@ 
    
    # $@--目标文件,$^--所有的依赖文件,$<--第一个依赖文件。每次$< $@ 代表的值就是列表中的
    #
    $(BIN) : $(COBJS) $(CPPOBJS)
    	$(CC) -o $(BIN) $(COBJS) $(CPPOBJS) $(LDFLAGS) $(LIBS) 
    	rm $(COBJS)
    	rm $(CPPOBJS)
    # 链接为最终目标
    
    
    #引入了.o文件对.c和.h的依赖情况。以后.h被修改也会重新生成,可看看.d文件内容即知道为何
    #引入了依赖就相当于引入了一系列的规则,因为依赖内容例如: 目录/xxx.o:目录/xxx.c 目录/xxx.h 也相当于隐含的引入了生成规则
    #故上面不能在出现如: $(OBJS) : $(DEF)之类。切记
    .PHONY:clean cleanall
    
    #清除所有目标文件以及生成的最终目标文件
    clean:            
    	rm $(BIN) #$(COBJS) $(CPPOBJS)
    #rm *.d
    cleanall:
    	rm $(BIN) $(COBJS) $(CPPOBJS)
    

    来源: 这里

    模板2

    #####################################################################
    ## file        : test makefile for build current dir .c            ##
    ## author      : jernymy                                           ##
    ## date-time   : 05/06/2010                                        ##
    #####################################################################
    
    CC      = gcc
    CPP     = g++
    RM      = rm -rf
    
    ## debug flag
    DBG_ENABLE   = 1
    
    ## source file path
    SRC_PATH   := .
    
    ## target exec file name
    TARGET     := test
    
    ## get all source files
    SRCS         += $(wildcard $(SRC_PATH)/*.c)
    
    ## all .o based on all .c
    OBJS        := $(SRCS:.c=.o)
    
    
    ## need libs, add at here
    LIBS :=
    
    ## used headers  file path
    INCLUDE_PATH := .
    
    ## used include librarys file path
    LIBRARY_PATH := /lib
    
    ## debug for debug info, when use gdb to debug
    ifeq (1, ${DBG_ENABLE}) 
        CFLAGS += -D_DEBUG -O0 -g -DDEBUG=1
    endif
    
    ## get all include path
    CFLAGS  += $(foreach dir, $(INCLUDE_PATH), -I$(dir))
    
    ## get all library path
    LDFLAGS += $(foreach lib, $(LIBRARY_PATH), -L$(lib))
    
    ## get all librarys
    LDFLAGS += $(foreach lib, $(LIBS), -l$(lib))
    
    
    all: clean build
    
    build:
        $(CC) -c $(CFLAGS) $(SRCS)
        $(CC) $(CFLAGS) -o $(TARGET) $(OBJS) $(LDFLAGS)
        $(RM) $(OBJS)
    
    clean:
        $(RM) $(OBJS) $(TARGET)
    

    来源: 这里

    模板3

    #===============================================================================
    #
    #     Filename: Makefile
    #  Description:
    #
    #        Usage: make              (generate executable                      )
    #               make clean        (remove objects, executable, prerequisits )
    #               make tarball      (generate compressed archive              )
    #               make zip          (generate compressed archive              )
    #
    #      Version: 1.0
    #      Created:
    #     Revision: ---
    #
    #       Author:
    #      Company:
    #        Email:
    #
    #        Notes: This is a GNU make (gmake) makefile.
    #               C   extension   :  c
    #               C++ extensions  :  cc cpp C
    #               C and C++ sources can be mixed.
    #               Prerequisites are generated automatically; makedepend is not
    #               needed (see documentation for GNU make Version 3.80, July 2002,
    #               section 4.13). The utility sed is used.
    #========================================== makefile template version 1.8 ======
    
    # DEBUG can be set to YES to include debugging info, or NO otherwise
    DEBUG          := NO
    
    # PROFILE can be set to YES to include profiling info, or NO otherwise
    PROFILE        := NO
    
    # ------------  name of the executable  ----------------------------------------
    EXECUTABLE      := zruijie
    
    # ------------  list of all source files  --------------------------------------
    SOURCES         := blog.c  eap_protocol.c  main.c  md5.c  zruijie.c
    
    # ------------  compiler  ------------------------------------------------------
    CC              := gcc
    CXX             := g++
    
    # ------------  compiler flags  ------------------------------------------------
    DEBUG_CFLAGS    := -Wall -O0 -g
    RELEASE_CFLAGS  := -Wall -O2
    
    # ------------  linker flags  --------------------------------------------------
    DEBUG_LDFLAGS    := -g
    RELEASE_LDFLAGS  :=
    
    ifeq (YES, ${DEBUG})
      CFLAGS       := ${DEBUG_CFLAGS}
      CXXFLAGS     := ${DEBUG_CXXFLAGS}
      LDFLAGS      := ${DEBUG_LDFLAGS}
    else
      CFLAGS       := ${RELEASE_CFLAGS}
      CXXFLAGS     := ${RELEASE_CXXFLAGS}
      LDFLAGS      := ${RELEASE_LDFLAGS}
    endif
    
    ifeq (YES, ${PROFILE})
      CFLAGS       := ${CFLAGS}   -pg -O3
      CXXFLAGS     := ${CXXFLAGS} -pg -O3
      LDFLAGS      := ${LDFLAGS}  -pg
    endif
    
    # ------------  additional system include directories  -------------------------
    GLOBAL_INC_DIR  =
    
    # ------------  private include directories  -----------------------------------
    LOCAL_INC_DIR   = 
    
    # ------------  system libraries  (e.g. -lm )  ---------------------------------
    SYS_LIBS        = /usr/lib/libpcap.a
    
    # ------------  additional system library directories  -------------------------
    GLOBAL_LIB_DIR  =
    
    # ------------  additional system libraries  -----------------------------------
    GLOBAL_LIBS     =
    
    # ------------  private library directories  -----------------------------------
    LOCAL_LIB_DIR   = 
    
    # ------------  private libraries  (e.g. libxyz.a )  ---------------------------
    LOCAL_LIBS      =
    
    # ------------  archive generation ---------------------------------------------
    TARBALL_EXCLUDE = *.{o,gz,zip}
    ZIP_EXCLUDE     = *.{o,gz,zip}
    
    # ------------  run executable out of this Makefile  (yes/no)  -----------------
    # ------------  cmd line parameters for this executable  -----------------------
    EXE_START       = no
    EXE_CMDLINE     =
    
    #===============================================================================
    # The following statements usually need not to be changed
    #===============================================================================
    
    C_SOURCES       = $(filter     %.c, $(SOURCES))
    CPP_SOURCES     = $(filter-out %.c, $(SOURCES))
    ALL_INC_DIR     = $(addprefix -I, $(LOCAL_INC_DIR) $(GLOBAL_INC_DIR))
    ALL_LIB_DIR     = $(addprefix -L, $(LOCAL_LIB_DIR) $(GLOBAL_LIB_DIR))
    GLOBAL_LIBSS    = $(addprefix $(GLOBAL_LIB_DIR)/, $(GLOBAL_LIBS))
    LOCAL_LIBSS     = $(addprefix $(LOCAL_LIB_DIR)/, $(LOCAL_LIBS))
    ALL_CFLAGS      = $(CFLAGS) $(ALL_INC_DIR)
    ALL_LFLAGS      = $(LDFLAGS) $(ALL_LIB_DIR)
    BASENAMES       = $(basename $(SOURCES))
    
    # ------------  generate the names of the object files  ------------------------
    OBJECTS         = $(addsuffix .o,$(BASENAMES))
    
    # ------------  generate the names of the hidden prerequisite files  -----------
    PREREQUISITES   = $(addprefix .,$(addsuffix .d,$(BASENAMES)))
    
    # ------------  make the executable (the default goal)  ------------------------
    $(EXECUTABLE):	$(OBJECTS)
    ifeq ($(strip $(CPP_SOURCES)),)
    								$(CC)  $(ALL_LFLAGS) -o $(EXECUTABLE) $(OBJECTS) $(LOCAL_LIBSS) $(GLOBAL_LIBSS) $(SYS_LIBS)
    else
    								$(CXX) $(ALL_LFLAGS) -o $(EXECUTABLE) $(OBJECTS) $(LOCAL_LIBSS) $(GLOBAL_LIBSS) $(SYS_LIBS)
    endif
    ifeq ($(EXE_START),yes)
    								./$(EXECUTABLE) $(EXE_CMDLINE)
    endif
    
    # ------------  include the automatically generated prerequisites  -------------
    # ------------  if target is not clean, tarball or zip             -------------
    ifneq ($(MAKECMDGOALS),clean)
    ifneq ($(MAKECMDGOALS),tarball)
    ifneq ($(MAKECMDGOALS),zip)
    include         $(PREREQUISITES)
    endif
    endif
    endif
    
    # ------------  make the objects  ----------------------------------------------
    %.o:		%.c
    				$(CC)  -c $(ALL_CFLAGS) $<
    
    %.o:		%.cc
    				$(CXX) -c $(ALL_CFLAGS) $<
    
    %.o:		%.cpp
    				$(CXX) -c $(ALL_CFLAGS) $<
    
    %.o:		%.C
    				$(CXX) -c $(ALL_CFLAGS) $<
    
    # ------------  make the prerequisites  ----------------------------------------
    #
    .%.d:   %.c
    				@$(make-prerequisite-c)
    
    .%.d:		%.cc
    				@$(make-prerequisite-cplusplus)
    
    .%.d:		%.cpp
    				@$(make-prerequisite-cplusplus)
    
    .%.d:		%.C
    				@$(make-prerequisite-cplusplus)
    
    #  canned command sequences
    #  echoing of the sed command is suppressed by the leading @
    
    define	make-prerequisite-c
    				@$(CC)   -MM $(ALL_CFLAGS) $< > $@.$$$$;            \
    				sed 's/\($*\)\.o[ :]*/\1.o $@ : /g' < $@.$$$$ > $@; \
    				rm -f $@.$$$$;
    endef
    
    define	make-prerequisite-cplusplus
    				@$(CXX)  -MM $(ALL_CFLAGS) $< > $@.$$$$;            \
    				sed 's/\($*\)\.o[ :]*/\1.o $@ : /g' < $@.$$$$ > $@; \
    				rm -f $@.$$$$;
    endef
    
    # ------------  remove generated files  ----------------------------------------
    # ------------  remove hidden backup files  ------------------------------------
    clean:
    								-rm -v  $(EXECUTABLE) $(OBJECTS) $(PREREQUISITES) *~
    
    # ------------ tarball generation ----------------------------------------------
    tarball:
    					@lokaldir=`pwd`; lokaldir=$${lokaldir##*/}; \
    					rm --force $$lokaldir.tar.gz;               \
    					tar --exclude=$(TARBALL_EXCLUDE)            \
    					    --create                                \
    					    --gzip                                  \
    					    --verbose                               \
    					    --file  $$lokaldir.tar.gz *
    
    # ------------ zip -------------------------------------------------------------
    zip:
    					@lokaldir=`pwd`; lokaldir=$${lokaldir##*/}; \
    					zip -r  $$lokaldir.zip * -x $(ZIP_EXCLUDE)
    
    install: $(EXECUTABLE)
    				@./install /usr/local/bin
    
    uninstall:
    				-rm /usr/local/bin/$(EXECUTABLE) /usr/local/bin/runruijie
    
    
    .PHONY: clean tarball zip
    
    # ==============================================================================
    # vim: set tabstop=2: set shiftwidth=2:
    

    来源: 这里