2012-06-02 41 views
5
  1. 是否有可能处理在Makfile警告作为错误(以及由此生成文件出口前进之前)如何将警告视为Makefile中的错误?

  2. 此外,是否有可能以滤除其中警告产生错误?

我用例:我想结合使用--warn-undefined-variables本,这样,当一个变量是不确定的,这是错误的一种很常见的源文件的Makefile将退出。显然,我不想手动检查每个变量,因为这是容易出错/繁琐的。我在这方面找不到任何东西,但这是一个非常重要/基本的功能。

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

+1

欢迎使用StackOverflow。这里的设计是“每个帖子有一个问题”,所以可以有一个明确的答案。多个问题意味着多个答案可以是正确的(我回答问题1,其他人回答问题2),并且不可能选择一个答案作为接受的答案。请查看[FAQ](http://stackoverflow.com/faq),以便您更熟悉SO的工作原理。谢谢。 –

+2

这对我来说似乎是一个合理的问题。可悲的是,我怀疑答案是1)否,2)没有意义,3)强硬。 – Beta

+0

它取决于'未定义变量'的意思,但没有值的变量('VAR =')可能非常重要(除了错误之外的任何内容)。我更频繁地遇到未使用的变量;这是一个曾经(大概)在makefile中使用的定义,但不再是这样。 –

回答

2

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) 

我没有试过,但我认为这应该是足够用fatal更换error

3

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

这里是它的错误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制作您想要检查的每个目标。

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

相关问题