makefile判断文件存在如下的两种方法:
1. 调用shell的函数进行判断
exist = $(shell if [ -f $(FILE) ]; then echo "exist"; else echo "notexist"; fi;) ifeq (exist, "exist") #do something here endif当然,这个方法很土,但是能够工作!!
2. 使用makefile的函数进行判断
ifeq ($(FILE), $(wildcard $(FILE))) #do something here endif
$(wildcard $(FILE))的意思是当前路径下的文件名匹配FILE的文件展开。
假设当前路径下存在a.c 和 b.c,那么执行src=$(wildcard *.c)
src的值就为a.c b.c;
如果不使用通配符,比如src=$(wildcard c.c);那么就是要展开当前路径下,文件名为c.c的文件,因为当前路径下文件不存在,因此src为空字符串。
在编写MAKEFILE的时候,如何判断一个文件是否存在
You need to escape the $.
EXISTED := $(shell test -e foo.c && echo $$?)
You can also use the wildcard function directly in make.
EXISTED := $(wildcard foo.c)
If foo.c doesn't exist, then $(wildcard ...) returns an empty string.
本文介绍了两种在Makefile中检查文件是否存在的方法:一种是通过调用Shell脚本实现,另一种则是直接利用Makefile内置的wildcard函数完成。通过这两种方式,可以有效地在构建过程中避免因文件缺失导致的错误。
3490

被折叠的 条评论
为什么被折叠?



