如何从 Makefile 设置 MAKEFLAGS,以删除默认的隐式规则

4 makefile

我尝试以下 makefile:

MAKEFLAGS += s
MAKEFLAGS += r

configure:
Run Code Online (Sandbox Code Playgroud)

然后,当我运行 make 时,出现以下错误,就好像它想根据某些默认隐式规则编译“配置”一样:

/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 13
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 13
Run Code Online (Sandbox Code Playgroud)

如果我运行:

make -r
Run Code Online (Sandbox Code Playgroud)

我没有收到上述错误,而是收到:

make: Nothing to be done for 'configure'.
Run Code Online (Sandbox Code Playgroud)

我从这里得到了定义 MAKEFLAGS 的想法:

如果您想要在每次运行“make”时设置某些选项,例如“-k”(*注意选项摘要:选项摘要。),“MAKEFLAGS”变量也很有用。您只需在您的环境中设置“MAKEFLAGS”的值即可。您还可以在 makefile 中设置“MAKEFLAGS”,以指定也对该 makefile 有效的其他标志。(请注意,您不能以这种方式使用“MFLAGS”。设置该变量只是为了兼容性;“make”不会以任何方式解释您为其设置的值。)

当“make”解释“MAKEFLAGS”的值(来自环境或来自 makefile)时,如果该值尚未以 1 开头,它首先会在前面添加一个连字符。然后它将值切成由空格分隔的单词,并解析这些单词,就像它们是命令行上给出的选项一样(除了 '-C'、'-f'、'-h'、'-o'、'- W' 及其长名称版本将被忽略;并且无效选项不会出现错误)。

Mad*_*ist 5

你没有说,但你可能使用的是太旧的 GNU make 版本。-r2013 年 10 月上旬发布的 GNU make 4.0 中添加了通过添加到MAKEFLAGSmakefile 内部来删除内置规则的功能。

重要的是要记住,当您查看 GNU 网站上的手册时,您正在查看最新版本的 GNU make 的文档。最好阅读您的发行版附带的文档,因为这将是与您正在使用的 GNU make 版本相关的正确版本的手册。

预计到达时间:

您必须在 中使用真正的标志MAKEFLAGS。您不能只使用单字母版本。唯一允许使用单字母选项的是变量值的第一个单词(如果标准不要求的话,我也会将其删除)。你写过:

MAKEFLAGS += s
MAKEFLAGS += r
Run Code Online (Sandbox Code Playgroud)

MAKEFLAGS它给出了(如果您运行 make 时没有其他选项)的值s r。Make 将在第一个单词中添加破折号,但不会在任何其他单词中添加破折号,因此这被解释为-s r并且r不是该-r选项。另外,如果您碰巧运行带有选项的 make,则假设make -kthenMAKEFLAGS将是,并且k s rmake 会将其解释为-k s r,然后该-s标志也不会被设置。

简而言之,当您想要修改时,只需在选项前面MAKEFLAGS添加破折号即可:

MAKEFLAGS += -s
MAKEFLAGS += -r
Run Code Online (Sandbox Code Playgroud)