由于经常需要在Linux下写程序,根据资料写了一个较为全面的Makefile模板,以后使用起来就比较方便了。可以根据选择编译生成可执行文件、静态链接库和动态链接库。
==========================================
MODULES := test
PROGNAME := test
CPPFLAGS := -g -Wall -fPIC -Wno-deprecated -D_FILE_OFFSET_BITS=64
USER_MACROS := DEBUG __PTHREADS_MUTEXES__ __linux__ __PTHREAD__
PY_INC := /usr/local/python/include/python2.6/
INC_DIR := ~/work/include
INC_PATH := $(INC_DIR)/inc-me $(INC_DIR)/inc-third $(INC_DIR) #$(PY_INC)
LIB_PATH := ~/work/lib
SHARE_PATH := $(LIB_PATH)/share
STATIC_PATH := $(LIB_PATH)/static
SHARE_LIBS := #pthread testlib
STATIC_LIBS := # libtestlib.a
SRC_PATH := .
OUTPUT_PATH := ../bin/
EXE_NAME := $(PROGNAME).exe
LIB_NAME := $(addprefix lib, $(PROGNAME))
SHARE_NAME := $(LIB_NAME).so
STATIC_NAME := $(LIB_NAME).a
SRC_DIRS := $(addprefix $(SRC_PATH)/,$(MODULES))
SRC_C := $(foreach SRC_DIRS,$(SRC_DIRS), $(wildcard $(SRC_DIRS)/*.c))
SRC_CPP := $(foreach SRC_DIRS,$(SRC_DIRS), $(wildcard $(SRC_DIRS)/*.cpp))
OBJ_C := $(patsubst %.c,%.o,$(SRC_C))
OBJ_CPP := $(patsubst %.cpp,%.o,$(SRC_CPP))
SRCS := $(SRC_C) $(SRC_CPP)
OBJS := $(OBJ_C) $(OBJ_CPP)
MAIN_SRC := $(SRC_PATH)/main.cpp
MAIN_OBJ := $(SRC_PATH)/main.o
CXX := g++
CC := gcc
AR := ar
ARFLAGS := -rc
.SECONDARY :%.o %(OBJS)
build: $(EXE_NAME)
mv $(EXE_NAME) $(OUTPUT_PATH)
static: $(STATIC_NAME)
mv $(STATIC_NAME) $(OUTPUT_PATH)
share: $(SHARE_NAME)
mv $(SHARE_NAME) $(OUTPUT_PATH)
touch:
touch $(SRCS) $(MAIN_SRC)
clean:
@echo "Clean ..."
@rm -f $(OBJS) $(MAIN_OBJ)
@rm -f $(EXE_NAME) $(STATIC_NAME) $(SHARE_NAME)
@rm -f *~ */*~
# Compiling to object files.
%.o:%.cpp
@echo "Compile $@ ($<)."
@$(CXX) $(CPPFLAGS) $(addprefix -D , $(USER_MACROS)) -I $(SRC_PATH) $(addprefix -I, $(INC_PATH)) -c $< -o $@
%.o:%.c
@echo "Compile $@ ($<)."
@$(CC) $(CPPFLAGS) $(addprefix -D , $(USER_MACROS)) -I $(SRC_PATH) $(addprefix -I, $(INC_PATH)) -c $< -o $@
# Linking to execute file.
$(EXE_NAME): $(OBJS) $(MAIN_OBJ)
@echo "Linking to execute ($@ : $(OBJS) $(MAIN_OBJ))."
@$(CXX) $(CPPFLAGS) -o $@ $(OBJS) $(MAIN_OBJ) $(addprefix -L,$(SHARE_PATH)/) $(addprefix -l,$(SHARE_LIBS)) $(addprefix $(STATIC_PATH)/,$(STATIC_LIBS))
# Archive to static library.
$(STATIC_NAME): $(OBJS)
@echo "Archive to static library ($@ [$(OBJS)])."
@${AR} ${ARFLAGS} $@ $(OBJS)
# Linking to share ( dynamic ) library.
$(SHARE_NAME): $(OBJS)
@echo "Linking to shared library ($@ [$(OBJS)])."
@$(CXX) $(CPPFLAGS) -o $@ $(OBJS) -shared $(addprefix -L,$(SHARE_PATH)/) $(addprefix -l,$(SHARE_LIBS)) $(addprefix $(STATIC_PATH)/,$(STATIC_LIBS))
========= 附件
|
文件: | Makefile.tar |
大小: | 4KB |
下载: | 下载 |
|
阅读(7627) | 评论(0) | 转发(0) |