Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 6 down vote accepted

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.

share|improve this answer
    
Thanks a lot Gnouc. Upvoted and chosen as best answer:) –  John Smith Feb 23 at 15:49
    
Note that this only applies to GNU make. POSIX defines $< only for inference rules and the .DEFAULT rule. –  nwellnhof Feb 23 at 20:31
    
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 Feb 24 at 1:53

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).

share|improve this answer
    
Thanks a lot Daniel. You were very helpful. Upvoted :) –  John Smith Feb 23 at 15:49
    
Are you sure that the " are allowed here? –  vonbrand Feb 24 at 1:47
    
@vonbrand yes, pretty sure. –  daniel kullmann Feb 24 at 5:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.