问题
I am trying to create a directory inside target based on a rule but for some reason the variable is not getting expanded i.e., $(OUT_DIR) is blank and mkdir -p does not work. Here is the code
target_%: /home/user/%/.file : file.c
export OUT_DIR = /home/user/$* ; \
mkdir -p $(OUT_DIR) ;\
.
.
.
After making the changes suggested by @Beta, here is how the code looks like
target_%: /home/user/%/.file : file.c
export OUT_DIR=/home/user/$* ; \
mkdir -p $${OUT_DIR} ;\
cd $${OUT_DIR} ; \
ln -s /home/user/file1.c file1.c.link ;\
When I run the code, i get the Error
/bin/sh: line 3: : command not found
on mkdir -p
command. I have played by removing ;\
and it works till mkdir $${OUT_DIR}
and cd $${OUT_DIR}
but i see the soft link created in the directory where the makefile is rather than in the OUT_DIR where it should be created.
I am also not sure when to use \
vs ; \
vs not using at all
. Any suggestion on that would be awesome
回答1:
Make variables and shell variables are not the same thing.
If you have something like $(OUT_DIR)
in the makefile, Make will expand it, and since you haven't defined it in the makefile, it expands to nothing. Your rule then looks like:
target_%: /home/user/%/.file : file.c
export OUT_DIR = /home/user/$* ; \
mkdir -p ;\
...
If the command (export ...
) you define OUT_DIR
as a shell variable. The way to expand a shell variable in the shell is with '$', like this:
...$OUT_DIR ...
But '$' is a special character in Make, so to use it as you intend, you must escape is with another '$':
target_%: /home/user/%/.file : file.c
export OUT_DIR = /home/user/$* ; \
mkdir -p $$OUT_DIR ;\
...
来源:https://stackoverflow.com/questions/49411762/variable-name-expansion-not-working-inside-target-in-gnu-makefile