makefile 独立@符号和目录关键字

makefile stand alone @ symbol and dir keyword

我想知道是否有人知道我的 makefile 中独立的 @ 符号和 'dir' 命令在这里(第二行和第三行)的作用:

$(BUILD)/%.o: %.cpp
    @mkdir -p $(dir $@) 
    $(CC) $(CFLAGS) -c $? -o $@

我明白 $@ 是什么,但是 GNU 手册中没有列出 @ 本身 (https://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html),而且我所有的搜索都在谈论 $@。

此外,在第三行,我知道 mkdir -p 创建了嵌套目录,但我希望有人可以详细说明 $(dir $@),特别是 "dir" 部分。

感谢您的宝贵时间。

这是一个消音器。当 @ 是命令的第一个字符时,执行该命令而不预先在标准输出中打印。

有关其他信息,请参阅 Makefile documentation

Normally make prints each line of the recipe before it is executed. We call this echoing because it gives the appearance that you are typing the lines yourself.

When a line starts with ‘@’, the echoing of that line is suppressed. The ‘@’ is discarded before the line is passed to the shell. Typically you would use this for a command whose only effect is to print something, such as an echo command to indicate progress through the makefile:

@echo About to make distribution files

When make is given the flag ‘-n’ or ‘--just-print’ it only echoes most recipes, without executing them. See Summary of Options. In this case even the recipe lines starting with ‘@’ are printed. This flag is useful for finding out which recipes make thinks are necessary without actually doing them.

The ‘-s’ or ‘--silent’ flag to make prevents all echoing, as if all recipes started with ‘@’. A rule in the makefile for the special target .SILENT without prerequisites has the same effect (see Special Built-in Target Names). .SILENT is essentially obsolete since ‘@’ is more flexible.

如果 @ 开始一个配方行,则命令本身不会回显。

$(dir $@) 扩展到目标 $@ 的目录部分。在这种情况下,如果目标是 $(BUILD)/rest/path/to/target.o.

,则为 $(BUILD)/rest/path/to/

来自GNU make manual, 5.2 Recipe Echoing

5.2 Recipe Echoing

Normally 'make' prints each line of the recipe before it is executed. We call this "echoing" because it gives the appearance that you are typing the lines yourself.

When a line starts with '@', the echoing of that line is suppressed. The '@' is discarded before the line is passed to the shell. Typically you would use this for a command whose only effect is to print something, such as an 'echo' command to indicate progress through the makefile:

 @echo About to make distribution files

来自 same, 8.3 Functions for File Names

8.3 Functions for File Names

Several of the built-in expansion functions relate specifically to taking apart file names or lists of file names.

Each of the following functions performs a specific transformation on a file name. The argument of the function is regarded as a series of file names, separated by whitespace. (Leading and trailing whitespace is ignored.) Each file name in the series is transformed in the same way and the results are concatenated with single spaces between them.

'$(dir NAMES...)' Extracts the directory-part of each file name in NAMES. The directory-part of the file name is everything up through (and including) the last slash in it. If the file name contains no slash, the directory part is the string './'. For example,

      $(dir src/foo.c hacks)

produces the result 'src/ ./'.