Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing in Makefiles: @echo vs $(info )

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?

like image 999
V. L. Avatar asked Nov 18 '17 15:11

V. L.


People also ask

What does @echo mean in Makefile?

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.

How do I print an environment variable in Makefile?

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.

What does $@ mean in Makefiles?

The variable $@ represents the name of the target and $< represents the first prerequisite required to create the output file.

How do I print a macro in Makefile?

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.


1 Answers

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.

like image 144
MadScientist Avatar answered Sep 19 '22 19:09

MadScientist