Makefile 例子和分析

2021-08-01 09:27:18 字數 1422 閱讀 9671

dir_inc=./inc

dir_src=./src

dir_obj=./obj

dir_bin=./bin

src=$(wildcard $/*.c)

obj=$(patsubst %.c,$/%.o,$(notdir $))

target=main

bin_target=$/$

cc=gcc

cflags = -g -wall -i$

$:$$(cc) $(obj)  -o $@

$/%.o:$/%.c

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

.phony:clean

clean:

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

解釋如下:

(1)makefile中的 符號 $@, $^, $< 的意思:

$@  表示目標檔案

$^  表示所有的依賴檔案

$$?  表示比目標還要新的依賴檔案列表

(2)wildcard、notdir、patsubst的意思:

wildcard : 擴充套件萬用字元

notdir : 去除路徑

patsubst :替換萬用字元

例如下圖例子所示:

輸出結果如下所示:

等於指定編譯當前目錄下所有.c檔案,如果還有子目錄,比如子目錄為inc,則再增加乙個wildcard函式,象這樣:

src = $(wildcard *.c) $(wildcard inc/*.c)

(3)gcc -i -l -l的區別:

gcc -o hello hello.c -i /home/hello/include -l /home/hello/lib -lworld

上面這句表示在編譯hello.c時-i /home/hello/include表示將/home/hello/include目錄作為第乙個尋找標頭檔案的目錄,

尋找的順序是:/home/hello/include-->/usr/include-->/usr/local/include

-l /home/hello/lib表示將/home/hello/lib目錄作為第乙個尋找庫檔案的目錄,

尋找的順序是:/home/hello/lib-->/lib-->/usr/lib-->/usr/local/lib

-lworld表示在上面的lib的路徑中尋找libworld.so動態庫檔案(如果gcc編譯選項中加入了「-static」表示尋找libworld.a靜態庫檔案)

makefile例子(經典)

相信在unix下程式設計的沒有不知道makefile的,剛開始學習unix平台 下的東西,了解了下makefile的製作,覺得有點東西可以記錄下。下面是乙個極其簡單的例子 現在我要編譯乙個hello world,需要如下三個檔案 1.print.h include void printhello 2...

簡單的makefile例子

1.2.3 簡單的示例 本小節開始我們在第一小節中提到的例子。此例子由3個頭檔案和8個c檔案組成。我們講述寫乙個簡單的makefile,來描述如何建立最終的可執行檔案 edit 此可執行檔案依賴於8個c原始檔和3個頭檔案。makefile檔案的內容如下 sample makefile edit ma...

Erlang的makefile 小例子

概要 erlang程式設計 第六章中的例子。hello.erl module hello export start 0 start io format hello world n shop.erl module shop export cost 1 cost oranges 5 cost news ...