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孟游的博客,转载请注明原文链接:https://www.cnblogs.com/mxdon/p/14279298.html

浙公网安备 33010602011771号