手头一个项目,需要编写项目的makefile
多目录结构:
csource/
├── common
│ └── sqlite3
├── inc
│ ├── curl
│ ├── lua
│ └── Protection
├── lib
│ ├── arm
│ └── linux
├── obj
├── out
│ ├── arm
│ └── linux
├── src
源码目录src,输出目录out,include目录inc,输入链接库目录lib,常用静态函数和sqlite3目录common
src:源代码目录
lib:链接库目录
out:输出目录
obj:中间文件目录
inc:include目录
common:常用函数目录
PLAT= none
CC=
CXX=
CFLAGS=
LDFLAGS=
MKDIR_P=mkdir -p
PLATS= linux arm
root= libroot.so
root_a= libroot.a
INC_DIR= ./inc
COM_DIR= ./common
SQL_DIR= ./common/sqlite3
LUA_DIR= ./inc/lua
PRO_DIR= ./inc/Protection
INCLUDE= -I$(LUA_DIR) -I$(INC_DIR) -I$(COM_DIR) -I$(SQL_DIR) -I$(PRO_DIR)
DIR_SRC= ./src
SRC = $(wildcard ${DIR_SRC}/*.cpp)
OBJ = $(patsubst %.cpp,${DIR_OBJ}/%.o,$(notdir ${SRC})) $(DIR_OBJ)/sqlite3.o
SO_TARGET = ${DIR_BIN}/${root}
LIB_TARGET= ${DIR_BIN}/${root_a}
# Targets start here.
default: $(PLAT)
none:
@echo "Please do 'make PLATFORM' where PLATFORM is one of these:"
@echo " $(PLATS)"
${SO_TARGET}:${OBJ}
$(CXX) $(OBJ) -o $@ $(LDFLAGS)
cp ${DIR_BIN}/${root} ./test/ -f
${LIB_TARGET}:${OBJ}
$(AR) $@ ${OBJ}
$(RANLIB) $@
dir:
$(MKDIR_P) $(DIR_OBJ) $(DIR_BIN);
all:$(SO_TARGET) $(LIB_TARGET)
ALL = dir all
linux:
$(MAKE) $(ALL) DIR_OBJ="./obj_linux/" DIR_BIN="./out/linux" \
CC="gcc" CXX="g++" AR="ar rcu" RANLIB="ranlib" \
CFLAGS="-Wno-write-strings -m32 -O2 -D_DEBUG -D_LINUX -fPIC" \
LDFLAGS="-O2 -shared -m32 -ldl -pthread -lrt -L./lib/linux -llua -lProtection -lz -lcurl"
arm:
$(MAKE) $(ALL) DIR_OBJ="./obj_arm/" DIR_BIN="./out/arm" \
CC="arm-linux-gnueabihf-gcc" CXX="arm-linux-gnueabihf-g++" \
AR="arm-linux-gnueabihf-ar rcu" RANLIB="arm-linux-gnueabihf-ranlib" \
CFLAGS="-Wno-write-strings -O2 -D_ARM -D__LINUX -fPIC" \
LDFLAGS="-O2 -shared -ldl -pthread -lrt -L./lib/arm -llua -lProtection -lz -lcurl"
# list targets that do not create files (but not all makes understand .PHONY)
.PHONY: all $(PLATS) default clean none
${DIR_OBJ}/%.o:${DIR_SRC}/%.cpp
$(CXX) $(CFLAGS) $(INCLUDE) -c $< -o $@
${DIR_OBJ}/sqlite3.o:${DIR_SRC}/sqlite3.c
$(CC) $(CFLAGS) $(INCLUDE) -c $< -o $@
.PHONY:clean
clean:
-find ${DIR_OBJ} -name *.o -exec rm -rf {} \;