include - 如果找不到某个包含文件,有没有办法告诉我的 makefile 输出自定义错误消息?

标签 include makefile

我有一个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/

相关文章:

c++ - 包括两个文件之间的冲突 C++

php - 换行符在 PHP 中意外出现

php - 文件中的返回语句不会停止类定义?

c - 具有两个可执行文件的递归生成文件

php - 我忘记了如何包含PHP文件(比如navbar.php,header.php)

c++ - Eclipse CDT : Symbol 'cout' could not be resolved

java - 进程 getInputStream() 永远不会结束

c++ - GLEW 在一个项目上正确链接,但在另一个项目上链接不正确(带有 MinGW Make 的 C++)

linux - Makefile 生成文件的数字列表

c++ - 如何在目录中查找并查找具有特定扩展名的所有文件