linux Makefile基礎知識

2021-06-28 14:37:04 字數 1530 閱讀 2353

2、makefile的核心:規則

目標:依賴

[tab]命令

命令被執行的條件:(1)沒有目標  (2)依賴檔案比目標檔案新

3、makefile的變數賦值

makefile中變數賦值有兩種方式:延時變數和立即變數。延時變數就是在使用到的時候才擴充套件,立即變數是在定義時就已經確定。

變數的定義語法形式如下:

immediate = deferred

immediate := immediate

4、makefile函式

函式呼叫的格式如下:

$(function arguments)

函式名和引數之間用空格或者tab隔開,如果有多個引數,它們之間用逗號隔開。這些空格和逗號不是引數值的一部分。

例如:$(patsubst pattern,replacement,text)  //尋找text中符合格式"pattern"的字,用"replacement"替換它們。

例項:$(patsubst %.c,%.o,x.c.c bar.c)

結果為:x.c.o bar.o

5、各類makefile的分析比較:

假如有如下三個檔案:a.h a.c b.c

a.h:

#define a 1

b.c:

#include

int test_fun()

a.c:

#include

#include "a.h"

int main()

第一種makefile:

test: a.c b.c a.h

gcc -o test a.c b.c

第二種makefile:

test: a.o b.o

gcc -o test a.o b.o

a.o: a.c

gcc -c -o a.o a.c

b.o: b.c

gcc -c -o b.o b.c

缺點:(1)如果依賴的檔案比較多,則需要為每一條依賴分配一條規則  (2)如果標頭檔案修改了不會引起編譯

第三種makefile:

test: a.o b.o

gcc -o test a.o b.o

a.o: a.c a.h

%.o: %.c

gcc -c -o $@ $<

缺點:每編譯乙個檔案,都得把依賴檔案在後面宣告,太繁瑣

第四種makefile:

objs := a.o b.o

test: $(objs)

gcc -o $@ $^

dep_files := (foreach f $(objs) $(f).d)

dep_files := wildcard $(dep_files)

ifneq ($(dep_files), )

include $(dep_files)

endif

%.o:%.c

gcc -wp,-md, [email protected] -c -o $@ $<

clean:

rm *.o test *.d

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...