Linux Makefile多目錄的編寫

2021-06-28 15:30:09 字數 2712 閱讀 8936

手頭乙個專案,需要編寫專案的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 plat= none

2 cc=

3 cxx=

4 cflags=

5 ldflags=

6 mkdir_p=mkdir -p

78 plats= linux arm

910 root= libroot.so

11 root_a= libroot.a

1213 inc_dir= ./inc

14 com_dir= ./common

15 sql_dir= ./common/sqlite3

16 lua_dir= ./inc/lua

17 pro_dir= ./inc/protection

18 include= -i$(lua_dir) -i$(inc_dir) -i$(com_dir) -i$(sql_dir) -i$(pro_dir)

19 dir_src= ./src

2021 src = $(wildcard $/*

.cpp)

22obj = $(patsubst %.cpp,$/%.o,$(notdir $)) $(dir_obj)/sqlite3.o

2324

so_target = $/$

25lib_target= $/$

2627

28# targets start here.

29default: $(plat)

3031

none:

32@echo "please do 'make platform' where platform is one of these:"

33@echo " $(plats)"

3435

$:$36

$(cxx) $(obj) -o $@ $(ldflags)

37cp $/$ ./test/ -f

3839

$:$

40$(ar) $@ $

41$(ranlib) $@

4243

dir:

44$(mkdir_p) $(dir_obj) $(dir_bin);

4546

all:$(so_target) $(lib_target)

4748

all = dir all

4950

linux:

51$(make) $(all) dir_obj="./obj_linux/" dir_bin="./out/linux" \

52cc="gcc" cxx="g++" ar="ar rcu" ranlib="ranlib" \

53cflags="-wno-write-strings -m32 -o2 -d_debug -d_linux -fpic" \

54ldflags="-o2 -shared -m32 -ldl -pthread -lrt -l./lib/linux -llua -lprotection -lz -lcurl"

5556

arm:

57$(make) $(all) dir_obj="./obj_arm/" dir_bin="./out/arm" \

58cc="arm-linux-gnueabihf-gcc" cxx="arm-linux-gnueabihf-g++" \

59ar="arm-linux-gnueabihf-ar rcu" ranlib="arm-linux-gnueabihf-ranlib" \

60cflags="-wno-write-strings -o2 -d_arm -d__linux -fpic" \

61ldflags="-o2 -shared -ldl -pthread -lrt -l./lib/arm -llua -lprotection -lz -lcurl"

6263

64# list targets that do not create files (but not all makes understand .phony)

65.phony: all $(plats) default clean none

6667

$/%.o:$/%.cpp

68$(cxx) $(cflags) $(include) -c $< -o $@

6970

$/sqlite3.o:$/sqlite3.c

71$(cc) $(cflags) $(include) -c $< -o $@

7273

.phony:clean

74clean:

75-find $ -name *.o -exec rm -rf {} \;

linux makefile檔案分析

cflags wall wstrict prototypes g fomit frame pointer ffreestanding all crt0.s leds.c arm linux gcc cflags c o crt0.o crt0.s arm linux gcc cflags c o l...

Linux Makefile由淺入深剖析

經過長時間學習linux makefile,於是和大家分享一下,看完本文你肯定有不少收穫,希望本文能教會你更多東西。假設我們有乙個程式由5個檔案組成,源 如下 main.c include mytool1.h include mytool2.h int main mytool1.c include ...

Linux Makefile簡單介紹

c exe linux 中 編譯器 gcc和g 預處理 e 彙編 s 編譯 c 鏈結 o hello gcc e hello.c o hello.i hello ls hello.c hello.i 來看一下hello.i的內容 巨集定義在與處理的時候就展開了 hello gcc s hello.i...