What is the difference between these two commands in makefiles:
@echo "Hello World"
$(info Hello World)
As it seems, echo
and info
print the same output, so where is the difference?
And when to use which one?
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.
To use it, just set the list of variables to print on the command line, and include the debug target: $ make V="USERNAME SHELL" debug makefile:2: USERNAME = Owner makefile:2: SHELL = /bin/sh.exe make: debug is up to date. Now you can print variables by simply listing them on the command line.
The variable $@ represents the name of the target and $< represents the first prerequisite required to create the output file.
There are various default macros. You can see them by typing "make -p" to print out the defaults. Most are pretty obvious from the rules in which they are used.
Well, echo
is a shell command. So if you put it in a recipe, a shell will be invoked to run it and the shell command will generate the output:
foo: ; @echo "Hello World"
runs /bin/sh -c 'echo "Hello World"'
. It can only be used in a recipe. It will work in any version of make, and with any POSIX shell. Because it invokes a shell, you may need to be concerned with quoting issues, etc. (not in this simple example of course).
info
is a GNU make function. It is handled directly by make: no shell is invoked. It can appear anywhere in a makefile, not just in a recipe. It is not portable to other versions of make. Because no shell is invoked, there are no quoting issues.
However, because info
is a make function it is parsed by make before the shell is invoked: that means it can't show shell variables that are set within a recipe; for example:
foo: ; @for i in a b c d; do $(info $$i); done
cannot work; you must use echo
here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With