9

I am seeing a makefile and it has the symbols $@ and $< in it. I have never seen them, and Google does not show any results about them. Do you know what these commands do?

2 Answers2

18

From make manpage:

$@ is:

The file name of the target of the rule. If the target is an archive member, then ‘$@’ is the name of the archive file. In a pattern rule that has multiple targets (see Introduction to Pattern Rules), ‘$@’ is the name of whichever target caused the rule's recipe to be run.

$< is:

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.

You can see complete manual here.

cuonglm
  • 153,898
7

To give an example: say you have a bunch of .c files that you want to compile to individual programs. You could write one make rule for each of them, or you could write one rule for all of them:

    %: %.c
            gcc -o "$@" "$<"

This rule means: if you have a make target (say program) and a .c file with the same basename (program.c), you can compile the .c file using gcc, giving the -o option with the name of the target (gcc -o program program.c).

daniel kullmann
  • 9,527
  • 11
  • 39
  • 46