What do the makefile symbols $@ and $< mean?
$@
is the name of the target being generated, and $<
the first prerequisite (usually a 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 toall
$<
evaluates tolibrary.cpp
$^
evaluates tolibrary.cpp main.cpp
From Managing Projects with GNU Make, 3rd Edition, p. 16 (it's under GNU Free Documentation License):
Automatic variables are set by
make
after a rule is matched. They provide access to elements from the target and prerequisite lists so you don’t have to explicitly specify any filenames. They are very useful for avoiding code duplication, but are critical when defining more general pattern rules.There are seven “core” automatic variables:
$@
: The filename representing the target.
$%
: The filename element of an archive member specification.
$<
: The filename of the first prerequisite.
$?
: The names of all prerequisites that are newer than the target, separated by spaces.
$^
: The filenames of all the prerequisites, separated by spaces. This list has duplicate filenames removed since for most uses, such as compiling, copying, etc., duplicates are not wanted.
$+
: Similar to$^
, this is the names of all the prerequisites separated by spaces, except that$+
includes duplicates. This variable was created for specific situations such as arguments to linkers where duplicate values have meaning.
$*
: The stem of the target filename. A stem is typically a filename without its suffix. Its use outside of pattern rules is discouraged.In addition, each of the above variables has two variants for compatibility with other makes. One variant returns only the directory portion of the value. This is indicated by appending a “D” to the symbol,
$(@D)
,$(<D)
, etc. The other variant returns only the file portion of the value. This is indicated by appending an “F” to the symbol,$(@F)
,$(<F)
, etc. Note that these variant names are more than one character long and so must be enclosed in parentheses. GNU make provides a more readable alternative with the dir and notdir functions.
The $@
and $<
are called automatic variables. The variable $@
represents the name of the target and $<
represents the first prerequisite required to create the output file.
For example:
hello.o: hello.c hello.h
gcc -c $< -o $@
Here, hello.o
is the output file. This is what $@
expands to. The first dependency is hello.c
. That's what $<
expands to.
The -c
flag generates the .o
file; see man gcc
for a more detailed explanation. The -o
specifies the output file to create.
For further details, you can read this article on linoxide about Linux Makefiles.
Also, you can check the GNU make
manuals. It will make it easier to make Makefiles and to debug them.
If you run this command, it will output the makefile database:
make -p
The $@
and $<
are special macros.
Where:
$@
is the file name of the target.
$<
is the name of the first dependency.