bash Makefile 语法:什么是 $(RM)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40907379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 15:29:26  来源:igfitidea点击:

Makefile syntax: what is $(RM)?

cbashmacosunixmakefile

提问by Keita

I saw the following Makefile online (here):

我在网上看到了以下 Makefile(这里):

hello:

clean:
    $(RM) hello

When there is a hello.c file in the same directory with the Makefile, makecommand in Terminal builds helloexecutable. When make cleanis run, helloexecutable is removed by rm -f helloinstead. So, $(RM) hellomeans rm -f hellohere.

当与 Makefile 同目录下有 hello.c 文件时make,终端中的命令构建hello可执行文件。当make clean运行时,hello可执行被删除rm -f hello,而不是。所以,这里的$(RM) hello意思rm -f hello

  • What does $(FOO) mean? Is it a special syntax of Makefile, or something bash command?
  • Can I run other commands as well as $(RM), like $(PWD)?
  • $(FOO) 是什么意思?它是 Makefile 的特殊语法,还是某种 bash 命令?
  • 我可以运行其他命令以及 $(RM),例如 $(PWD) 吗?

回答by Danh

It's a Makefile variable. There're explicit variable (which is defined inside Makefile) or implicit variable (defined by make, can be override by you).

它是一个 Makefile 变量。有显式变量(在 Makefile 中定义)或隐式变量(由 make 定义,可以由您覆盖)。

The list of implicit variables can be found by:

可以通过以下方式找到隐式变量列表:

make -p

some of the most common variables can be found at: 10.3 Variables Used by Implicit Rules

一些最常见的变量可以在以下位置找到:10.3 隐式规则使用的变量

You can expand variable by $(NAME)or ${NAME}

您可以通过$(NAME)或扩展变量${NAME}

回答by Jens

$(RM)is a reference to a "make" variable (or macroin POSIX lingo). These come in two syntax flavors, with identical semantics: $(NAME)and ${NAME}(and there are one letter macros not needing parentheses, such as $a).

$(RM)是对“make”变量(或POSIX 术语中的)的引用。它们有两种语法风格,具有相同的语义:$(NAME)${NAME}(并且有不需要括号的单字母宏,例如$a)。

POSIX specifiesmany variables, errrrr, macros, to have a default:

POSIX 指定了许多变量, errrrr, macros,有一个默认值:

MAKE=make
AR=ar
ARFLAGS=-rv
YACC=yacc
YFLAGS=
LEX=lex
LFLAGS=
LDFLAGS=
CC=c99
CFLAGS=-O 1
FC=fort77
FFLAGS=-O 1
GET=get
GFLAGS=
SCCSFLAGS=
SCCSGETFLAGS=-s

Interestingly, RMisn't one of them. The default value for the RMvariable in your make implementation apparently is rm -f(and your make runs in non-POSIX mode by default).

有趣的RM是,不是其中之一。RMmake 实现中变量的默认值显然是rm -f(并且默认情况下您的 make 以非 POSIX 模式运行)。

Note that while $(PWD)has the same syntax as a shell command substitution (and ${PWD}the same as a shell parameter), they are completely different things. To answer your question, no, you can't expect $(PWD)as a "make" macro to run the pwdutility or expand to the current working directory. An undefined macro will be expanded to an empty string without "make" even raising an eyebrow.

请注意,虽然$(PWD)与 shell 命令替换具有相同的语法(与${PWD}shell 参数相同),但它们是完全不同的东西。要回答您的问题,不,您不能期望$(PWD)作为“make”宏来运行该pwd实用程序或扩展到当前工作目录。一个未定义的宏将被扩展为一个空字符串,而不会出现“make”甚至扬起眉毛。

回答by kabanus

It's a make file variable. Previously or in your env you have RM="rm -f"syntax depending on the shell or if it's in the make itself, and now you're just executing.

这是一个 make 文件变量。以前或在您的 env 中,您的RM="rm -f"语法取决于 shell 或者它是否在 make 本身中,而现在您只是在执行。

You can run pwd, but to use $(PWD), you need to set PWD="pwd".

可以运行pwd,但要使用$(PWD),需要设置PWD="pwd"