Sunday, September 1, 2019

常用的 Makefile 自動變數


$@ is the name of the file being generated, and $< the first prerequisite (usually the source file). You can find a list of all these special variables in the GNU Make manual.
For example, consider the following declaration:
all: library.cpp main.cpp
In this case:
  • $@ evaluates to all
  • $< evaluates to library.cpp
  • $^ evaluates to library.cpp main.cpp

-----------------------------------------------------------------------------
Automatic Variables

$@
Target name (目標條件名), the value at the left side of the comma. 
foo.o: foo.c
     echo $@     Expected Result = foo.o

$<
The first prerequisite (第一條件名), the first value at the right side of the comma. 
foo.o: foo.c
     echo $<     Expected Result = foo.c

$^
All the prerequisites (全個 list). The whole list of values at the right side of the comma.
foo.o: food.c drinks.c life.c
     echo $^     Expected Result = food.c drinks.c life.c