Search code examples
makefilegnu-makeautotools

How to interpret the $@ symbol in a makefile loop


In a makefile I have this piece of code

all clean dep depend print:
    for dir in $(DIRS); do \
        if $(MAKE) $(MAKE_FLAGS) -C $$dir $@; then \
            true; \
        else \
            exit 1; \
        fi; \
    done

What is the meaning of $@ in the line

if $(MAKE) $(MAKE_FLAGS) -C $$dir $@; then \

I know this is an Automatic Variable that matches the file name of the target of the rule. Here the target appears to be a command like cancel:

cancell:
    rm -rf *.o

Solution

  • It is an Automatic variable that expands to the name of the target which caused the recipe to run. In your case, if you type make all, it expands to all. If you type make all clean, it will run the recipes twice -- the first time, $@ will expand to all, and the second time it will expand to clean.

    See documentation here