---
title: "makefile中如何判断一个文件是否存在"
author: "scm002"
date: "2012-05-05T17:01:08Z"
tags: []
categories: ["编程语言"]
slug: "makefile"
draft: false
lastmod: "2012-05-05T17:02:12Z"
id: 1512890
url: "https://scm002.iteye.com/blog/1512890"
---

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的时候，如何判断一个文件是否存在](/cqzj119/article/details/1631668)

 

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.