在 g++ 参数中使用时“$<”是什么意思?

What does "$<" mean when used in a g++ argument?

我正在使用一个包含示例应用程序的库。示例 makefile 在参数中包含 $<

all:test.cpp
    g++ -Wl,--no-as-needed -o Example $<
clean:
    rm -f SampleApp11

我查过这个 tutorialspoint

$< the name of the related file that caused the action.

另一个 website 指出:

this is a suffix replacement rule for building .o's from .c's it uses automatic variables $<: the name of the prerequisite of the rule(a .c file) and $@: the name of the target of the rule (a .o file) (see the gnu make manual section about automatic variables) .c.o: $(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

我还是一头雾水,这是什么意思?

这实际上与编译器无关,它是 Makefile 语法的一部分,并且在编译器之前被替换为 运行.

在您的示例中,它是 all: 目标之后的 first 依赖项(文件)- test.cpp.

Makefile 的基本功能是在 dependency 发生变化时创建一个 target

target: dependency.cpp
    rule to create target (using dependency.cpp)

通常 $< 是编译器的 输入 $@ 输出 .

有点像这个(不是有效的 Makefile):

$@: $<
    g++ -o $@ $<

我记得它们的方式是 @ 类似于靶(在打靶练习中),而 < 类似于箭头。所以我想象一个 arrow 指向 target:

@ <-------- (think "Robin Hood")

YMMV

GNU make's automatic variables之一。

The name of the first prerequisite. If the target got its recipe from an implicit rule, this will be the first prerequisite added by the implicit rule (see Implicit Rules).

先决条件是 rule:

中列出的文件
targets : prerequisites
        recipe

例如,在以下规则中,第一个先决条件是 test.c 文件:

my_executable: test.c precompiled.o
    g++ -o my_executable $<

等同于:

my_executable: test.c precompiled.o
    g++ -o my_executable test.c

precompiled.o 是第二个先决条件(隐含预编译目标文件)。