如果您准备为每个目标添加依赖项,则可以将警告转换为错误。
这里是它的错误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
制作您想要检查的每个目标。
如果有人知道如何自动执行此操作,请将其键入。
欢迎使用StackOverflow。这里的设计是“每个帖子有一个问题”,所以可以有一个明确的答案。多个问题意味着多个答案可以是正确的(我回答问题1,其他人回答问题2),并且不可能选择一个答案作为接受的答案。请查看[FAQ](http://stackoverflow.com/faq),以便您更熟悉SO的工作原理。谢谢。 –
这对我来说似乎是一个合理的问题。可悲的是,我怀疑答案是1)否,2)没有意义,3)强硬。 – Beta
它取决于'未定义变量'的意思,但没有值的变量('VAR =')可能非常重要(除了错误之外的任何内容)。我更频繁地遇到未使用的变量;这是一个曾经(大概)在makefile中使用的定义,但不再是这样。 –