变量名称扩展在GNU Makefile中的目标内部不起作用 [英] variable name expansion not working inside target in GNU Makefile

查看:72
本文介绍了变量名称扩展在GNU Makefile中的目标内部不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据规则在目标内部创建目录,但是由于某种原因,变量没有得到扩展,即$(OUT_DIR)为空并且mkdir -p不起作用.这是代码

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)     ;\
    .
    .
    .

完成@Beta建议的更改后,代码如下所示

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 ;\  

运行代码时,出现错误

/bin/sh: line 3:        : command not found

mkdir -p命令上按

.我玩过删除;\的操作,直到mkdir $${OUT_DIR}cd $${OUT_DIR}都可以使用,但是我看到在makefile所在的目录中创建的软链接,而不是在应创建该文件的OUT_DIR中创建的软链接.

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.

我也不确定何时使用\ vs ; \ vs not using at all.对此的任何建议都很棒

I am also not sure when to use \ vs ; \ vs not using at all. Any suggestion on that would be awesome

推荐答案

制作变量和 shell 变量不是同一回事.

Make variables and shell variables are not the same thing.

如果makefile中有类似$(OUT_DIR)的内容,Make将对其进行扩展,并且由于您尚未在makefile中对其进行定义,因此它不会扩展为任何内容.您的规则如下:

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      ;\
    ...

如果命令(export ...)将OUT_DIR定义为外壳变量.在外壳程序中扩展外壳程序变量的方法是使用"$",例如:

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 ...

但是'$'是Make中的特殊字符,因此要按预期使用它,您必须转义与另一个'$':

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     ;\
    ...

这篇关于变量名称扩展在GNU Makefile中的目标内部不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆