makefile

Generate each source file to be one bin file

  • Makefile

    CC     = g++
    CFLAGS += -g -Wall
    SRC    = $(wildcard *.cc)
    #OBJS   = $(SRC:.cc=.o)
    BINS   = $(SRC:.cc=)
    LIBS   = 
    
    all:$(BINS)
    
    # bin files depend on *.cc files
    %:%.cc
        $(CC) $(CFLAGS) -o $@ $<
    
    .PHONY: clean
    clean:
        rm -f *.o $(BINS)
    

Generate all source files to be on bin file

  • Makefile

    CC     = g++
    CFLAGS += -g -Wall
    SRC    = $(wildcard *.cc)
    OBJS   = $(SRC:.cc=.o)
    #BINS   = $(SRC:.cc=)
    TARGET = test
    LIBS   = 
    
    all:$(TARGET)
    
    # bin files depend on *.cc files
    $(TARGET):$(OBJS)
        $(CC) $(CFLAGS) -o $@ $<
    
    .PHONY: clean
    clean:
        rm -f *.o $(TARGET)
    

Auto run all Makefile in all sub-directory

  • Makefile

    ########################################################
    # Makefile
    # History:
    #        2013/04/17 Dennis Create
    ########################################################
    
    include ./Make.defines
    
    subdirs := $(sort $(subst /,,$(dir $(wildcard */*))))
    
    all:
        $(foreach N,$(subdirs),make -C $(N);)
    
    .PHONY: clean
    clean:
        $(foreach N,$(subdirs),make -C $(N) clean;)
        rm -f $(CLEANFILES)
    
  • Make.define

    # "make" is executed in all the subdirectories of this directory
    
    CC = gcc
    CFLAGS  = -I../lib -g -o2 -D_REENTRANT -Wall
    LIBS = ../libunp.a -lpthread
    
    LIBUNP_NAME = ../libunp.a
    
    LIB_OBJS = error.o wrapsock.o str_echo.o writen.o wrapunix.o wraplib.o str_cli.o \
               readline.o wrapstdio.o tcp_listen.o tcp_connect.o signal.o readn.o \
               write_fd.o read_fd.o
    
    CLEANFILES = *.o *.a tags cscope.out
    
  • epoll/Makefile

    CC     = gcc
    CFLAGS = -Wall -g2
    LIB_CPP= -lstdc++
    
    TARGET = t1 t2 t3
    
    all:$(TARGET)
    
    t1: t1.c
        $(CC) $(FLAGS) -o $@ $^
    
    t2: t2.c
        $(CC) $(FLAGS) -o $@ $^
    
    t3: t3.cpp
        $(CC) $(FLAGS) $(LIB_CPP) -o $@ $^
    
    .PHONY:clean
    clean:
        rm -f *.o $(TARGET)
    

Example

  • Makefile

    ###########################################################
    # Makefile
    #
    # History:
    # 2013-06-03 Dennis Create
    # 2013-06-04 Dennis Make it correct and better
    ###########################################################
    
    .SUFFIXES : .x .o .c .cpp .s
    ROOTDIR    = $(shell pwd)
    
    CC = g++
    
    TARGET = cppscsi
    
    #LIBDIR    = $(ROOTDIR)/lib
    #SHARLIB:=$(shell find $(LIBDIR) -name '*.so')
    
    INCPATH = $(ROOTDIR)
    INCPRG  =-I$(INCPATH) \
             -I$(INCPATH)/common \
             -I$(INCPATH)/digest \
             -I$(INCPATH)/datasegment \
             -I$(INCPATH)/login
    
    INCDIR  = $(INCPRG)
    
    CFLAGS += $(INCDIR)
    
    #SOURCES:=$(shell find $(ROOTDIR) -name '*.cpp' -o -name '*.c')
    SOURCES:=$(shell find $(ROOTDIR) -maxdepth 1 -name '*.cpp' -o -name '*.c')
    CPPSRC :=$(notdir $(SOURCES))
    OBJECTS:=$(CPPSRC:.cpp=.o)
    
    # search all subdirs
    #subdirs := $(sort $(subst /,,$(dir $(wildcard */*))))
    subdirs := ./common \
               ./digest \
               ./data \
               ./datasegment \
               ./login
    
    # generate subdirs object name
    OBJS_OTHER_SRC := $(shell find $(subdirs) -name '*.cpp')
    OBJS_OTHER := $(OBJS_OTHER_SRC:.cpp=.o)
    
    all: $(TARGET)
    
    $(TARGET): $(OBJECTS)
        @echo "**************** build subdir ****************"
        $(foreach N,$(subdirs),make -C $(N);)
        @echo "**************** link target ****************"
        $(CC) -o $(TARGET) $(OBJECTS) $(OBJS_OTHER) $(SHARLIB) $(CFLAGS)
    
    $(OBJECTS): %.o:%.cpp
        $(CC) -c $< $(CFLAGS)
    
    .PHONY: clean
    clean:
        $(foreach N,$(subdirs),make -C $(N) clean;)
        rm -f *.o $(TARGET) 
    
  • login/Makefile

    ###########################################################
    # Makefile
    #
    # History:
    # 2013-06-05 Dennis Create
    ###########################################################
    
    CC= g++
    
    SRC= LoginRequestParser.cpp \
         LoginResponseParser.cpp \
         ISID.cpp
    
    FLAGS = -I../ -I../common
    
    OBJS := $(SRC:.cpp=.o)
    
    all: $(OBJS)
    
    $(OBJS): %.o:%.cpp
        $(CC) $(FLAGS) -c $<
    
    .PHONY: clean
    clean:
        rm -f *.o 
    

An complex example

  • Makefile

    ###########################################################
    # Makefile
    # 
    # History:
    #    v1.2  2014-08-24 Dennis  Generate install script
    #    v1.1  2014-07-12 Dennis  Auto generate version info
    #    v1.0  2014-06-19 Dennis  Create
    ###########################################################
    
    CC     = gcc
    CFLAGS+= -Wall -g
    INC    = -I./
    SRC    = main.c \
             other_sources.c
    OBJ    = $(SRC:.c=.o)
    # NOTE: gcc start loading libraries from the right, 
    #       so $(LIB) should be on the far right of gcc 
    LIB    = -lpthread
    TARGET = myapp
    
    all: gen-version $(TARGET)
    
    $(TARGET): $(OBJ)
        $(CC) $(CFLAGS) $(INC) -o $@ $^ $(LIB)
    
    gen-version:
        @./update-version.sh
    
    # Generate install script
    release:
        @echo "Generate release script"
        @tar zcf myapp.tar.gz myapp config.conf
        @cat install.sh myapp.tar.gz >myapp-install.run
        @chmod +x myapp-install.run
    
    .PHONY:clean
    clean:
        -rm -f $(TARGET) $(OBJ) myapp.tar.gz myapp-install.run
    
  • install.sh

    #!/bin/bash
    echo ""
    echo "Self Extracting Installer"
    echo ""
    
    export TMPDIR=`mktemp -d /tmp/selfextract.XXXXXX`
    
    ARCHIVE=`awk '/^__ARCHIVE_BELOW__/{print NR+1; exit 0;}' $0`
    
    tail -n+$ARCHIVE $0 | tar xzv -C $TMPDIR
    
    CDIR=`pwd`
    cd $TMPDIR
    
    #./installer   # 改为压缩包中安装程序的地址
    
    # Make sure we are root
    if [ `id | sed -e 's/(.*//'` != "uid=0" ]; then
      echo "Sorry, you need super user privileges to run this script."
      exit 1
    fi
    
    #UNINST=
    #
    # Check for and uninstall any previous version.
    #
    if [ -x "${UNINST}" ]; then
      echo "Removing previous installation..."
      #
    fi
    
    INSTALL_DIR=/opt/app/myapp/ 
    if [ ! -d $INSTALL_DIR ]; then
        mkdir -p $INSTALL_DIR
    fi
    cp -f myapp $INSTALL_DIR
    cp -f config.conf $INSTALL_DIR
    
    cd $CDIR
    rm -rf $TMPDIR
    
    exit 0
    
    __ARCHIVE_BELOW__
    
  • update-version.sh

    sh -c "sed -i -e '/__MYAPP_AUTO_VERSION__/ {
        s/=.*$/= \"git commit $(git rev-parse HEAD), compiled $(date '+%Y-%m-%d %H:%M')\"\;/
    }' version.c"
    sh -c "sed -i -e '/__MYAPP_AUTO_DATE__/ {
            s/=.*$/= \"$(date '+%Y')\"\;/
    }' version.c"
    
  • version.c

    /*===============================================================
     * Copyright (C) 2014 All rights reserved.
     * 
     * 文件名称:version.h
     * 创 建 者:Matrix207
     * 创建日期:2014年07月12日
     * 描    述:
     *
     * 更新日志:
     *
     ================================================================*/
    #ifndef _VERSION_H_
    #define _VERSION_H_
    
    char __MYAPP_AUTO_VERSION__[] = "git commit 93785a3926fd2be190a2154b639d8eb7a83fa2e3, compiled 2014-08-24 13:08";
    char __MYAPP_AUTO_DATE__[] = "2014";
    #endif /* _VERSION_H_ */
    

Another example

  • Makefile

    #################################################################
    # Copyright (C) 2014 All rights reserved.
    # 
    # 文件名称:Makefile
    # 创 建 者:Dennis
    # 创建日期:2014年12月11日
    # 描    述:
    #
    # 更新日志:
    #   v0.1  20141211  Dennis  创建
    ##################################################################
    DATE_TIME=$(shell date '+%y%m%d%H%M%S')
    DATE_TIME2=$(shell date '+%Y-%m-%d %H:%M')
    
    DIR_INC = ./common
    DIR_SRC = ./
    DIR_BIN = ./
    CC      = gcc
    INC     = -I./common -I./inc
    SRC     = $(wildcard ${DIR_SRC}/*.c)
    COMMON  = $(wildcard ${DIR_INC}/*.c)
    LIB     = -lm
    #LIB     = -lpthread
    CFLAGS  = -g -Wall -I${DIR_INC}
    
    TARGET = ${DIR_BIN}/mytest
    
    all: PREPARE_DIR gen-version $(TARGET)
    
    $(TARGET): $(SRC) $(COMMON)
        $(CC) $(CFLAGS) $(INC) -o $@ $^ $(LIB)
    
    PREPARE_DIR:
        mkdir -p $(DIR_BIN)
    
    gen-version:
        ./update-version.sh
    
    .PHONY:clean
    clean:
        rm -f $(TARGET)