Monday, December 3, 2012

Computing Makefile variable on assignment - Stack Overflow


In a Makefile, I'm trying to assign the result of a shell command to a variable:
TMP=`mktemp -d /tmp/.XXXXX`

all:
    echo $(TMP)
    echo $(TMP)
but
$ make Makefile all
is echoing 2 different values, eg:
/tmp/.gLpm1T
/tmp/.aR4cDi
What is the syntax for mktemp to be computed on variable assignment?
Thank you.
share|improve this question

57% accept rate
Was this post useful to you?     

3 Answers


up vote10down voteaccepted
It depends on the flavour of make. With GNU Make, you can use := instead of = as in
TMP:=$(shell mktemp -d /tmp/.XXXXX)
Edit As pointed out by Novelocrat, the = assignment differs from := assignment in that values assigned using = will be evaluated during substitution (and thus, each time, the variable is used), whereas := assigned variables will have their values evaluated only once (during assignment), and hence, the values are fixed after that. See the documentation of GNU Make for a more detailed explanation.
In order for the value to be truelly constant after assignment, though, the it should not contain any parts, which might be special to the shell (which make calls in order to actually run the update rules, etc.) In particular, backticks are best avoided. Instead, use GNU make's built-in shell function and similar to achieve your goals.
share|improve this answer
Variables assigned with = are evaluated at each substitution. Variables assigned with := are evaluated at assignment, as you desire. – Novelocrat Sep 16 '09 at 22:37
Hi Dirk, It does not seem to work: pastie.org/619606 Is there a syntax error in my Makefile? Thanks – abernierSep 16 '09 at 23:04
Edited answer again. I think, the problem is the use of the backticks; for make, the value (incl. the backticks) is essentially "constant" and will get pasted as is at the point of use -- and then, the backticks will be seen by the shell at each point of use. Just a theory, though. – Dirk Sep 16 '09 at 23:12
Ok, finally found solution, thanks to you and your link: I have to use := for assignment operator, but also $(shell mktemp -d /tmp/.XXXXX) It works now, thank you so much! – abernier Sep 16 '09 at 23:16
I've accepted your answer, please add the $(shell ) tip ;) – abernier Sep 16 '09 at 23:17
feedback


Computing Makefile variable on assignment - Stack Overflow

'via Blog this'

No comments:

Post a Comment