Makefile的原則是倒序着寫, 如test.c 編到test.bin:
test.bin(目標) : test(依賴)
arm-eabi-objcopy -O binary test test.bin //這裏寫有依賴文件後怎樣生成目標文件
test : test.o
arm-eabi-ld test.o -o test //這裏寫有test.o後怎樣生成test
test.o : test.c
arm-eabi-gcc test.c -c -o test.o // test.c 生成test.o
makefile執行時可以指定目標: make 目標 //不指定目標則執行第一個目標
makefile裏的執行語句前加"@",表示此語句的信息不輸出
.PHONY : 目標 //可避免目標名與文件名同名時的衝突
如:
6 .PHONY : clean hello
7 clean:
8 @echo “clean”
9
10 hello:
11 @echo “hello world”
$< 表示第一個依賴文件
$^ 表示所有依賴文件
$@ 表示目標
$(CURDIR) //獲取當前目錄的路徑
目標 : 另一目標 //makefile裏的目標可以依賴另一個目標
CFILES := ${wildcard .c} //獲取當前目錄下的.c文件
OBJS := ${patsubst %.c, %.o, $(CFILES)} //把*.c的文件名替換成*.o
make TARGET=hello //執行makefile時,改變裏面變量TARGET的值
makefile裏給子目錄的makefile傳遞參數:
export CROSS_COMPILE := arm-eabi-
export TOPDIR := $(CURDIR)