如何将警告视为Makefile中的错误?

tim*_*our 7 warnings makefile undefined build-error

  1. 是否可以将警告视为Makfile中的错误(因此在Makefile继续之前退出)

  2. 此外,是否可以过滤掉哪个警告产生错误?

我的用例:我想--warn-undefined-variables与此结合使用,以便在未定义变量时Makefile将退出,这是一个非常常见的错误来源.显然我不想手动检查每个变量,因为这很容易出错/繁琐.我在这方面找不到任何东西,但这是一个非常重要/基本的功能.

注意:我不是在寻找-Werror哪个是不适用于我的用例的gcc特定命令.

And*_*aam 5

如果准备将依赖项添加到每个目标,则可以将警告变为错误。

这是一个包含错误的生成文件(“ SRCS”而不是“ SRC”):

# Turn on the warning we want
MAKEFLAGS += --warn-undefined-variables

# Make sure MAKECMDGOALS is defined, so it doesn't cause an error itself
ifndef MAKECMDGOALS
MAKECMDGOALS = all
endif

SRC=hello.c

all: compile

# Fails if the Makefile contains any warnings.
# Run this Makefile with the same goals, but with the -n flag.
# Grep for warnings, and fail if any are found.
no-make-warnings:
    ! make -n $(MAKECMDGOALS) 2>&1 >/dev/null | grep warning

# Targets you want to check must depend on no-make-warnings
compile: no-make-warnings
    gcc -o hello $(SRCS)
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我看到以下内容:

$ make
! make -n all 2>&1 >/dev/null | grep warning
Makefile:17: warning: undefined variable `SRCS'
make: *** [no-make-warnings] Error 1
Run Code Online (Sandbox Code Playgroud)

您只需要使要检查的每个目标都取决于目标即可no-make-warnings

如果有人知道如何自动执行此操作,请发出提示。


Rei*_*eek 3

make 的标准版本不支持您正在寻找的内容。但是,构建您自己的 make 版本来满足您的用例应该不难。

查看make 3.82的源代码,查看warn_undefinedvariable.h中的宏:

214 /* Warn that NAME is an undefined variable.  */
215 
216 #define warn_undefined(n,l) do{\
217                               if (warn_undefined_variables_flag) \
218                                 error (reading_file, \
219                                        _("warning: undefined variable `%.*s'"), \
220                                 (int)(l), (n)); \
221                               }while(0)
Run Code Online (Sandbox Code Playgroud)

error我没有尝试过这个,但我认为用替换应该足够了fatal