【问题标题】:How to treat a warning as an error in a Makefile?如何将警告视为 Makefile 中的错误?
【发布时间】:2012-06-02 04:59:28
【问题描述】:
  1. 是否可以将警告视为 Makfile 中的错误(从而在 Makefile 继续之前退出)

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

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

注意:我不是在寻找 -Werror,这是一个不适用于我的用例的 gcc 特定命令。

【问题讨论】:

  • 欢迎来到 StackOverflow。这里的设计是“一个帖子一个问题”,所以可以有一个明确的答案。多个问题意味着多个答案可能是正确的(我回答问题 1,其他人回答问题 2),并且不可能选择一个答案作为接受的答案。请查看FAQ,以便您更熟悉 SO 的工作原理。谢谢。
  • 这对我来说似乎是一个合理的问题。可悲的是,我怀疑答案是 1) 否,2) 没有实际意义,3) 强硬。
  • 这取决于您所说的“未定义变量”是什么意思,但是没有值的变量 (VAR = ) 可能非常重要(除了错误之外的任何内容)。我更经常遇到未使用的变量;这是一个曾经(可能)在 makefile 中使用但不再使用的定义。
  • --error-undefined-variables 将非常有用!扩展从未在其他地方提及的变量总是在我的 makefile 中是一个错误。

标签: makefile warnings undefined build-error


【解决方案1】:

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

这是一个包含错误的 make 文件(“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)

当我运行它时,我看到了这个:

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

你只需要让你想要检查的每个目标都依赖于目标no-make-warnings

如果有人知道如何自动执行此操作,请加入。

【讨论】:

    【解决方案2】:

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

    查看make 3.82的源码,查看variable.h中的宏warn_undefined

    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)
    

    我没有尝试过,但我认为将error替换为fatal就足够了。

    【讨论】:

      猜你喜欢
      • 2011-03-15
      • 2011-01-05
      • 2018-09-28
      • 1970-01-01
      • 2012-01-13
      • 2022-08-03
      • 2017-01-07
      • 2010-11-05
      • 2013-07-06
      相关资源
      最近更新 更多