Linux Makefile與shell指令碼區別

2021-08-11 05:29:15 字數 2236 閱讀 1925

在makefile可以呼叫shell指令碼,但是makefile和shell指令碼是不同的。本文試著歸納一下makefile和shell指令碼的不同。

1、 shell中所有引用以$打頭的變數其後要加{},而在makefile中的變數是以$打頭的後加()。例項如下:

makefile:

path="/data/"

subpath=$(path)

shell:

path="/data/"

subpath=$

2、makefile中所有以$打頭的單詞都會被解釋成makefile中的變數。如果你需要呼叫shell中的變數(或者正規表示式中錨定句位$),都需要加兩個$符號($$)。makfile例項如下:

path="/data/"

all:

echo $

echo $$path

例子中的第乙個$引用的是makefile中的變數,而不是shell中的path環境變數,後者引用的是shell中的path環境變數。

3、萬用字元區別

shell 中萬用字元*表示所有的字元

makefile 中萬用字元%表示所有的字元

4、在makefile中只能在target中呼叫shell指令碼,其他地方是不能輸出的。比如如下**就是沒有任何輸出:

var="hello"

echo "$var"  

all:

.....

以上**任何時候都不會輸出,而且還會報錯,如下:makefile:*** command commence before first target.stop,因為沒有在target內。如果上述**改為如下:

var="hello"

all:

echo "$var"   

.....

以上**,在make all的時候將會執行echo命令,同時必須注意echo "$var"之前必須有乙個table,這樣makefile才會認為其為一條command,如果沒有table會報錯如下:makefile:*** missing separator.stop.

5、在makefile中執行shell命令,一行建立乙個程序來執行。這也是為什麼很多makefile中有很多行的末尾都是「;  \」,以此來保證**是一行而不是多行,這樣makefile可以在乙個程序中執行,例如:

subdir=src example

all:

@for subdir in $(subdir); \

do\echo "building "; \

done

上述可以看出for迴圈中每行都是以」; \」結尾的。

6、獲取當前目錄

path=`pwd` 注意是``,不是''

7、shell總=兩邊不允許有空格,makfile中=兩邊允許有空格。

在makefile中寫shell**有點詭異,和一般的shell語法不太一樣,如果不了解,看makefile會莫名其妙。下面總結了一些。

1:在makefile檔案的目標項冒號後的另起一行的**才是shell**

,並且另起一行的**前面要有table製表符,這樣才會認為是shell**,

例如:xx = xx1         //這裡是makefile**

yy :xx = xx2   //這是是makefile**,

makefile允許變數賦值時,「=」號兩邊留空格

yy:                //這是目標項

xx=xx3        //只有這裡是shell** ,

shell不允許『=』號兩邊有空格,xx=xx3前面至少有乙個table製表符

2: 有乙個例外:

xx=$(shell 這裡的**也是shell**)

例如:$(echo hello)

2:makefile中的shell,每一行是乙個程序,不同行之間變數值不能傳遞。所以,makefile中的shell不管多長也要寫在一行,例如:

subdir=src example

all:

@for subdir in $(subdir); /      // 這裡往下是一行shell

do/echo "building " $$subdir; /

done

3:makefile中的變數以$開頭, 所以,為了避免和shell的變數衝突,shell的變數以$$開頭

$$1}')

例子2:上例中

$$subdir就是shell中的變數, 而$(subdir)是makefile的中的變數

shell程式設計:

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