how to print variable value in Makefile?
Hello everyone,
I want to print variable value of Makefile, but it seems that simple echo command is not working. This is my Makefile, -------------------- Foo=newdir echo $Foo -------------------- when I execute make, it prints out the following error message, -------------------- Makefile:2: *** missing separator. Stop. -------------------- I am using Red Hat Linux 9.0. Does anyone know what is the correct approach to print out the value of a variable? thanks in advance, George |
It would be extremely beneficial to read the documentation on make. It's very thorough and not too terribly technical in the language it uses. I say that because the Makefile you present demonstrates a basic mistake.
Makefiles aren't like shell scripts. You need to create targets (objects that make tries to create). The make command only looks for commands to execute related to a specific target, and also requires the command to be indented with a tab character as the first character on the line. The documentation describes it a little better. To give you a Makefile that will accomplish what you want, use this: Code:
Foo=newdir |
Dark_Helmet.
Quote:
Another question is that, what is the use of .PHONY target? I think the following Makefile will work the same, Code:
Foo=newdir regards, George |
The ".PHONY" tag is a special "target" that tells the Makefile there are no dependencies to check against. In short, any target listed as a phony will always execute the commands for the target no matter what. Contrast that behavior with a normal target (one with .c or .h file dependencies) where the commands to create the target are only executed if one or more of the dependencies has changed.
The documentation I was referring to is located here: Make manual |
Thanks Dark_Helmet,
Quote:
regards, George |
No dependencies to check for modification.
When make is invoked, it compares the timestamp for the target's last modification against the timestamps of the target's dependencies. If the target's modification timestamp is the most recent, then the target is "up to date" and make will not perform any commands for the target. If, however, one (or more) of the target's dependencies has a more recent modification timestamp than the target itself, the target needs o be updated to reflect the changes made since the last time make was run for that target. So, what's the timestamp for a non-existent target? When "make print_vars" is issued, we have no file representing our up-to-date target. The commands do not create a file named print_vars. So there's nothing for make to get a timestamp from. Not only that, but the print_vars target has no dependencies either. So make couldn't even look at those timestamps. So what should make do? I think the documentation says that in this case, make will do nothing for the target. The makefile needs to specify ".PHONY" to tell make to ignore its normal timestamp-check and execute the commands regardless. It's the same concept behind the "make clean" target found in many source code tarballs. The "clean" target does not create a "clean" file -- the commands are meant to accomplish the side-effect of deleting all the object files and executables for the project (to allow a from-scratch recompile). |
Thanks Dark_Helmet,
Quote:
.PHONY: all I think it is not the case you mentioned that there is no existing file to check timestamp. Another question is that, what do you mean "accomplish the side-effect of deleting all the object files and executables for the project"? regards, George |
Try:
Code:
FOO = newdir make -pns | grep FOO |
Thanks bigearsbilly,
Quote:
regards, George |
make -pns (chortle) prints out variables and macros.
for example I have a messy tcl script for evaluating all variables using make -pns so for a makefile: Code:
AA = ${PWD} Code:
billym.primadtpdev>makelook| grep AAA Code:
#!/bin/sh |
Thanks bigearsbilly,
Quote:
regards, George |
printing variables, maybe ;)
|
Thanks bigearsbilly,
Quote:
regards, George |
All times are GMT -5. The time now is 01:14 PM. |