Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing variable from within Makefile

Tags:

I have the following code in my makefile:

S_RES=$(shell cat output)  echo -e "Serial result = \t" $(S_RES) 

Basically, I want to store the output of the shell command cat output in the S_RES variable, and then echo that variable to the screen (with some explanatory text in front of it). I also want to be able to use the variable later on in my program. I thought I had followed the instructions given in various StackOverflow questions, but it doesn't seem to work.

like image 988
robintw Avatar asked May 17 '11 22:05

robintw


People also ask

How do I print a 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 is @echo 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 can you display the value of the variables created using make command?

@enchanter You can use echo, but you would need to put it outside the foreach loop, like this: @echo -e $(foreach var,$(. VARIABLES),"$(var)=$($(var))\n") . $(foreach...) concatenates all of the values from the loop, and then executes the result.


1 Answers

If simple space instead of escape sequence \t is allowed, and your make is GNU make 3.81 or higher, $(info) is available.
For example:

$(info Serial result = $(S_RES)) 

If your make's version is 3.80 or lower, $(warning) might meet the purpose. However, warning prints line number etc. too.

EDIT: For your information, the following makefile outputs abc on my GNU make 3.81.

A := $(shell echo abc) $(info $(A)) 
like image 118
Ise Wisteria Avatar answered Sep 19 '22 03:09

Ise Wisteria