Linux Makefile多目录的编写

手头一个项目,需要编写项目的makefile

多目录结构:

csource/  

├── common
│   └── sqlite3
├── inc
│   ├── curl
│   ├── lua
│   └── Protection
├── lib
│   ├── arm
│   └── linux
├── obj
├── out
│   ├── arm
│   └── linux
├── src

源码目录src,输出目录out,include目录inc,输入链接库目录lib,常用静态函数和sqlite3目录common

makefile如下:

 1 test = libtest.so
 2 test_a  = libtest.a
 3 
 4 LDFLAGS = -O2 -shared -m32 -ldl -pthread -lrt -L./lib/linux -llua -lz -lcurl
 5 CFLAGS = -Wno-write-strings -m32 -O2 -D__LINUX -D_DEBUG -fPIC
 6 CXX     = g++
 7 CC = gcc
 8 AR      = ar rcu
 9 RANLIB  = ranlib
10 
11 
12 INC_DIR = ./inc
13 COM_DIR = ./common
14 SQL_DIR = ./common/sqlite3
15 LUA_DIR = ./inc/lua
16 PRO_DIR = ./inc/Protection
17 INCLUDE = -I$(LUA_DIR) -I$(INC_DIR) -I$(COM_DIR) -I$(SQL_DIR) -I$(PRO_DIR)
18 DIR_SRC = ./src
19 DIR_OBJ = ./obj
20 DIR_BIN = ./out/linux
21 
22 SRC = $(wildcard ${DIR_SRC}/*.cpp)
23 OBJ = $(patsubst %.cpp,${DIR_OBJ}/%.o,$(notdir ${SRC})) $(DIR_OBJ)/sqlite3.o
24 
25 SO_TARGET = ${DIR_BIN}/${test}
26 LIB_TARGET= ${DIR_BIN}/${test_a}
27 
28 all:$(SO_TARGET) $(LIB_TARGET)
29 
30 ${SO_TARGET}:${OBJ}
31         $(CXX) $(OBJ) -o $@ $(LDFLAGS)
32 
33 ${LIB_TARGET}:${OBJ}
34         $(AR) $@ $(objects)
35         $(RANLIB) $@
36 
37 ${DIR_OBJ}/%.o:${DIR_SRC}/%.cpp
38         $(CXX) $(CFLAGS) $(INCLUDE) -c $< -o $@
39 
40 ${DIR_OBJ}/sqlite3.o:${DIR_SRC}/sqlite3.c
41         $(CC) $(CFLAGS) $(INCLUDE) -c $< -o $@
42 
43 .PHONY:clean
44 clean:
45         -find ${DIR_OBJ} -name *.o -exec rm -rf {} \;

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。