我有一个configure
生成 config.inc
的脚本包含一些变量定义和 makefile
的文件使用导入这些配置
include config.inc
令我困扰的是,如果用户尝试直接运行 makefile 而没有先运行配置,他们会收到一条无用的错误消息:
makefile:2: config.inc: No such file or directory
make: *** No rule to make target 'config.inc'. Stop.
有没有办法让我产生更好的错误消息,指示用户首先运行配置脚本,而不采用从内部生成完整 makefile 的 autoconf 策略 configure
?
最佳答案
当然,没问题;只需做这样的事情:
atarget:
echo here is a target
ifeq ($(wildcard config.inc),)
$(error Please run configure first!)
endif
another:
echo here is another target
include config.inc
final:
echo here is a final target
请注意,这绝对是 GNU make 特有的;没有可移植的方法来做到这一点。
编辑:上面的例子可以正常工作。如果文件 config.inc
存在,那么它将被包含在内。如果文件 config.inc
不存在,那么 make 将在读取 makefile 时退出(由于 error
函数),并且永远不会到达 include
行,这样就不会有关于丢失包含文件的模糊错误。这就是原发帖者所要求的。
EDIT2:这是一个运行示例:
$ cat Makefile
all:
@echo hello world
ifeq ($(wildcard config.inc),)
$(error Please run configure first!)
endif
include config.inc
$ touch config.inc
$ make
hello world
$ rm config.inc
$ make
Makefile:5: *** Please run configure first!. Stop.
关于include - 如果找不到某个包含文件,有没有办法告诉我的 makefile 输出自定义错误消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29422858/