Makefile Command Substitution Problem

Makefile command substitution problem

You're missing the part where you need to use shell in order to actually call external programs when defining a variable.

LAST_CONFIG:=$(shell cat config.tmp)

Makefile: why command substitution can't work in $(shell) function?

You need to escape the dollar:

JAVABIN = $(shell dirname $$(which java))

See 6.1 Basics of Variable References.


The specific error message you received was caused by the fact that the $(which java) piece expanded to the empty string, since it was an undefined variable. Hence the dirname system command ended up seeing no arguments, in which case it complains of a "missing operand".

command substitution doesn't work with echo in makefile

If you want the shell that Make invokes to receive the following:

echo "Created at: $(date)" >> README.md

Then, you need to escape the $ with another $ inside the rule:

README.md:
echo "Created at: $$(date)" >> README.md

Otherwise, the Make's variable date is expanded and that will be what echo gets as argument, since $(date) in a makefile expands the variable date.


Out of intererst

Note that, if the Make's variable date is defined as below, it will however work as expected without quoting the $ in the rule:

date = $$(date)

README.md:
echo "Created at: $(date)" >> README.md

The reason is that the variable date (used in the rule's recipe) will be expanded by Make to $(date) and that will be passed to the shell.

Makefile command substitution does not accept parameters

Removing the superfluous quotes from the variable should do it:

GO_BUILD     := go build -ldflags "-s -w" -a -installsuffix cgo

Otherwise, the shell (that make spawns) sees this command line:

'go build ...' -o cli ./cli 

It correctly treats the whole string go build ... as argv[0] and tries to find it as an executable.

Error: no source files when using command substitution

As RenaudPacalet said, I had to put an extra $ infront of the second line.

Substitute variable in sed inside Makefile

Use double quotes so that sed would expand variables.

Use a separator different than / since the replacement contains /

QUERY=$(cat queries/table_knowledge.rq | sed "s|%contributor%|<${l}>|g") ;

Test:

$ test="http://test.com"
$ echo "?link dcterms:contributor %contributor%" > source.file
$ x=$(cat source.file|sed "s|%contributor%|<${test}>|g")
$ echo $x
?link dcterms:contributor <http://test.com>

Impossible to execute shell command from Makefile

$ has special meaning in makefiles, you need to double it to pass it literally to the shell.

stop:
kill $$(ps aux | grep '[p]ython service/logdata.py' | awk '{print $$2}')

Makefile variable substitution sometimes ignored

It's usually not a good idea to rely on environmental variables in a makefile. Making the value explicit in the makefile, or specifying it in the call (e.g. make CUDA=...) is actually the correct way to go.

If you still want to use the value from the environment, I don't know why your makefile isn't working, but you can try this:

 CUDA_BIN := $(shell echo $$CUDA_BIN)


Related Topics



Leave a reply



Submit