【C/C++】Makefile之automatic variables

$@、$<、$^就是automatic variables,具体可参考GNU Make manual: Automatic-Variables。

下面用一个简单的例子做一个快速的介绍:

hello: hello_main.c hello.c hello.h
         gcc -o $@ $^
  • $@: 目标文件,即hello
  • $^ : 所有的源文件,即hello_main.c hello.c hello.h
  • $<: 第一个源文件,即hello_main.c ($<在什么情况下用后续再研究一下)

Attention:

When the first dependency is a variable representing a list, $< is evaluated after it is expanded. [2]

LIST = lib1.cpp lib2.cpp
all: $(LIST) main.cpp

这里$< 只是指 lib1.cpp

参考资料:

  1. GNU Make manual: Automatic-Variables, gnu.org.
  2. What do the makefile symbols $@ and $< mean?, stackoverflow.

你可能感兴趣的:(【C/C++】Makefile之automatic variables)