ITeye博客

makefile中如何判断一个文件是否存在

scm002 · 2012-05-05 17:01 · Makefile · 编程语言 · Markdown

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.

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。