弄了一个简单的makefile文件,记录下,方便以后使用。
##################################################################
# to build the program, just use "make"
# to clean the objs and bin files, use "make clean"
##################################################################
ROOT_DIR := $(shell pwd)
MODULES := . api server tools log examples cxx11
PROJECT_NAME := MyServer
EXTRALIBS := #-lpthread
LIBS := pthread log4cplus
#src and include path info
PROJECT_PATH := $(ROOT_DIR)
OUTPUT_PATH := $(PROJECT_PATH)/bin
INCS_PATH := $(PROJECT_PATH)/src
LIBS_PATH := $(PROJECT_PATH)/lib
SRC_PATH := $(PROJECT_PATH)/src
SRC_DIRS := $(addprefix $(SRC_PATH)/, $(MODULES))
EXTRINCPATH := -I/usr/local/log4cplus/include
EXTRLIBPATH := -L/usr/local/log4cplus/lib
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH
FINAL_OUTPUT := $(addprefix $(OUTPUT_PATH)/,$(PROJECT_NAME))
USER_MACROS := _DEBUG LINUX SERVER
CFLAGS := -g -fPIC -Wall
CC := gcc
%.o:%.c
$(CC) $(CFLAGS) $(addprefix -D, $(USER_MACROS)) -I$(INCS_PATH) $(addprefix -I,$(INCS_PATH)) -c $< -o $@
CPPFLAGS := -std=c++11 -g -fPIC -Wall
CXX := g++
%.o:%.cpp
$(CXX) $(CPPFLAGS) $(addprefix -D, $(USER_MACROS)) -I$(INCS_PATH) $EXTRINCPATH $(addprefix -I,$(INCS_PATH)) -c $< -o $@
#c source complile
C_SRC := $(foreach SRC_DIR, $(SRC_DIRS), $(wildcard $(SRC_DIR)/*.c))
C_OBJS := $(patsubst %.c,%.o,$(C_SRC))
#c++ source complile
CPP_SRC := $(foreach SRC_DIR, $(SRC_DIRS), $(wildcard $(SRC_DIR)/*.cpp))
CPP_OBJS := $(patsubst %.cpp,%.o,$(CPP_SRC))
OBJS := $(C_OBJS) $(CPP_OBJS)
.SECONDARY :%.o $(OBJS)
$(FINAL_OUTPUT):$(OBJS)
$(CXX) $(CPPFLAGS) -o $@ $(OBJS) $(addprefix -L,$(LIBS_PATH)) $EXTRLIBPATH $(addprefix -l,$(LIBS)) $(EXTRALIBS)
build_all:$(FINAL_OUTPUT)
clean:
rm -rf $(C_OBJS) $(CPP_OBJS) $(TEMP_OUTPUT) $(FINAL_OUTPUT)
rm -rf $(OUTPUT_PATH)/$(PROJECT_NAME)