无法禁用"默认"后缀规则,在Gnu-Make中

时间:2015-09-13 04:16:47

标签: c gcc makefile gnu-make

我的Makefile,如下所示:

.SUFFIXES:foo


# Disable "default" suffix-rules.
MAKEFLAGS=-r


# A rule without a recipe!
all.o::

# File 'all.c' is "ought to exist".
all.c:
    touch '$@'


执行,我得到:

# Gnu-make still applies the "default" suffix-rule '.c.o'. Wrong?
$ make
cc    -c -o all.o all.c


为什么我不能禁用后缀规则?

2 个答案:

答案 0 :(得分:1)

定义.SUFFIXES的先决条件会添加到已知后缀列表中。它不会删除已知的后缀。作为一种例外,如果您为没有先决条件的.SUFFIXES定义规则,则取消所有隐式规则:

$ cat Makefile
.SUFFIXES:
foo.c:
    touch $@
$ rm -f foo.* ; make foo.o
make: *** No rule to make target `foo.o'.  Stop.

注意:要取消.o .c %.o: %.c 的隐式规则,还可以添加不带配方的模式规则:

MAKEFLAGS

关于MAKEFLAGS = -r,根据文件,其主要用途似乎是将选项传递给子品牌。但是,文档还指出:

您还可以在makefile中设置MAKEFLAGS,以指定对该makefile也应该有效的其他标志

因此,从逻辑上讲,在makefile中定义make -r应该与invoquing $ cat Makefile MAKEFLAGS += -r foo.c: touch $@ $ rm -f foo.* ; make --version ; make foo.o GNU Make 3.81 Copyright (C) 2006 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. This program built for i686-pc-linux-gnu touch foo.c cc -c -o foo.o foo.c 具有相同的效果。这是您期望的行为。显然,情况并非如此(使用GNU make版本3.81和3.82):

$ rm -f foo.* ; make --version ; make foo.o
GNU Make 3.82
Built for i686-pc-linux-gnu
Copyright (C) 2010  Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
touch foo.c
cc    -c -o foo.o foo.c

$ rm -f foo.* ; make --version ; make foo.o
GNU Make 4.0
Built for i686-pc-linux-gnu
Copyright (C) 1988-2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
make: *** No rule to make target 'foo.o'.  Stop.

但它适用于GNU make 4.0和4.1:

$ rm -f foo.* ; make --version ; make foo.o
GNU Make 4.1
Built for i686-pc-linux-gnu
Copyright (C) 1988-2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
make: *** No rule to make target 'foo.o'.  Stop.

summary.default

显然,错误已经修复。

答案 1 :(得分:1)

问题是添加-r标志只会删除默认后缀规则设置。当您使用.SUFFIXES: foo时,您会在现有设置中添加新的后缀规则。执行此操作后,.SUFFIXES的值不再与默认值相同,因此添加-r标志不会更改它。如果您想在Savannah上提交,可以将此视为有用的增强请求。

如果您想使用新的后缀规则而不是任何预定义的规则,您可以使用POSIX标准永久可用的方法;首先删除所有现有的,然后添加新的:

.SUFFIXES:
.SUFFIXES: foo