Makefile 學習與使用
Makefile 學習與使用
- $@ 目標文件
- $^ 所有依賴文件
- $< 第一個依賴文件
- $? 修改后的依賴文件

VPATH和vpath
- VPATH = dir1 dir2...
在當前目錄下找不到文件時會到VPATH下逐個尋找 - vpath [file] [dir1 dir2...]
- 依次在
dir下尋找file - 將含有
file的文件夾置空 - 清楚所有已設置的
vpath
- 依次在
example:
.c file in src,
.h file in include
nothing in current directory
vpath %.c src
vpath %.h include
main:main.o list1.o list2.o
gcc -o $@ $<
main.o:main.c
gcc -o $@ $^
list1.o:list1.c list1.h
gcc -o $@ $<
list2.o:list2.c list2.h
gcc -o $@ $<
等同于:
VPATH=src include
main:main.o list1.o list2.o
gcc -o $@ $<
main.o:main.c
gcc -o $@ $^
list1.o:list1.c list1.h
gcc -o $@ $<
list2.o:list2.c list2.h
gcc -o $@ $<
Makefile 隱含規則
Makefile 條件判斷
- ifeq (a,b)
- ifneq 'a' 'b'
- ifdef a
- ifndef a
- else endif
Makefile 偽目標
.PHONY:clean
clean:
rm -rf *.o test
Makefile 函數
- 字符串處理函數
$(patsubst %.c,%.o,1.c 2.c 3.c)把.c file替換成.o file
$(subst fuck,like,fuck you)輸出like you
$(strip a b c)輸出abc
$(filter %.c %.o,1.c 2.o 3.s)過濾.c & .o file輸出1.c 2.o
$(filter-out 1.c 2.o ,1.o 2.c 3.s)輸出3.s
$(sort foo bar foo lost)輸出bar foo lost而且去重
$(word num,text1 text2 text3)輸出the NO.num text
- 文件名操作函數
$(dir file/directory...)獲取目錄名
$(notdir file/directory...)獲取文件名
$(suffix file/directory...)獲取文件后綴
$(basename file/directory...)獲取文件前綴
$(addsuffix suffix,file/directory...)給文件或目錄添加后綴
$(addprefix prefix,file/directory...)給文件或目錄添加前綴
$(join a b,1 2 3)連接函數,輸出a1 b2 3
$(wildcard *.c *.h)通配符函數
- 其他常用函數
$(foreach var,list,text)把參數list中的單詞逐一取出放到參數var所指定的變量中,然后再執行text所包含的表達式。
$(if condition,then-part [,else-part])
$(call expression,parm1,parm2,parm3,...)
Makefile 命令回顯
@cmd
Makefile 文件包含
include filenames出錯退出
-include filenames出錯也不退出
本文來自博客園,作者:游客aka孟游的博客,轉載請注明原文鏈接:http://www.rzrgm.cn/mxdon/p/14279298.html

浙公網安備 33010602011771號