makefilegnusymbols

makefile stand alone @ symbol and dir keyword


I was wondering if anyone knows what the stand alone @ symbol and the 'dir' command in my makefile does here (second & third line) :

$(BUILD)/%.o: %.cpp
    @mkdir -p $(dir $@) 
    $(CC) $(CFLAGS) -c $? -o $@

I understand what $@ is however @ by itself is not listed in the GNU handbook (https://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html) and all my searches talk about the $@.

Furthermore, in the third line, I understand that mkdir -p creates nested directories but I was hoping if someone can please elaborate on $(dir $@) specifically the "dir" part.

Thank you for your time.


Solution

  • It is a silencer. When @ is the first character of a command, the said command is executed without being printed in stdout beforehand.

    See Makefile documentation for additional info:

    Normally make prints each line of the recipe before it is executed. We call this echoing because it gives the appearance that you are typing the lines yourself.

    When a line starts with ‘@’, the echoing of that line is suppressed. The ‘@’ is discarded before the line is passed to the shell. Typically you would use this for a command whose only effect is to print something, such as an echo command to indicate progress through the makefile:

    @echo About to make distribution files
    

    When make is given the flag ‘-n’ or ‘--just-print’ it only echoes most recipes, without executing them. See Summary of Options. In this case even the recipe lines starting with ‘@’ are printed. This flag is useful for finding out which recipes make thinks are necessary without actually doing them.

    The ‘-s’ or ‘--silent’ flag to make prevents all echoing, as if all recipes started with ‘@’. A rule in the makefile for the special target .SILENT without prerequisites has the same effect (see Special Built-in Target Names). .SILENT is essentially obsolete since ‘@’ is more flexible.