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 Answers 2

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.

2
  • Note that this only applies to GNU make. POSIX defines $< only for inference rules and the .DEFAULT rule.
    – nwellnhof
    Commented Feb 23, 2014 at 20:31
  • 1
    You should also consider $^, which is all prerequisites. This is useful in rules for prog: a.o b.o c.o as $(CC) -o $@ $^
    – vonbrand
    Commented Feb 24, 2014 at 1:53
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).

3
  • Are you sure that the " are allowed here?
    – vonbrand
    Commented Feb 24, 2014 at 1:47
  • @vonbrand yes, pretty sure. Commented Feb 24, 2014 at 5:21
  • Inside the rule this is just shell so the quotes should be a good thing.
    – sqqqrly
    Commented Feb 22, 2023 at 15:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .