Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell 是否在make文件中使用$$?_Shell_Makefile - Fatal编程技术网

Shell 是否在make文件中使用$$?

Shell 是否在make文件中使用$$?,shell,makefile,Shell,Makefile,我有以下结构 home |-- Makefile |-- H1 | |-- Makefile | `-- h1.c |-- H2 | |-- Makefile | `-- h2.c |-- main.c H1/Makefile h1.o:h1.c gcc -c h1.c h2.o:h2.c gcc -c h2.c DIR = h1 h2 main.out:main.o for i in $(DIR);\ do

我有以下结构

home
|-- Makefile
|-- H1
|   |-- Makefile
|   `-- h1.c
|-- H2
|   |-- Makefile
|   `-- h2.c
|-- main.c
H1/Makefile

h1.o:h1.c
        gcc -c h1.c
h2.o:h2.c
        gcc -c h2.c
DIR = h1 h2
main.out:main.o
        for i in $(DIR);\
        do (cd $$i; make);\ //1.why () used here to encolose 2 commands and $$ before the i
        done
        gcc -o main.out main.o ./h1/h1.o ./h1/h1.o

main.o:main.c
        gcc -c main.c
H2/Makefile

h1.o:h1.c
        gcc -c h1.c
h2.o:h2.c
        gcc -c h2.c
DIR = h1 h2
main.out:main.o
        for i in $(DIR);\
        do (cd $$i; make);\ //1.why () used here to encolose 2 commands and $$ before the i
        done
        gcc -o main.out main.o ./h1/h1.o ./h1/h1.o

main.o:main.c
        gcc -c main.c
home/Makefile

h1.o:h1.c
        gcc -c h1.c
h2.o:h2.c
        gcc -c h2.c
DIR = h1 h2
main.out:main.o
        for i in $(DIR);\
        do (cd $$i; make);\ //1.why () used here to encolose 2 commands and $$ before the i
        done
        gcc -o main.out main.o ./h1/h1.o ./h1/h1.o

main.o:main.c
        gcc -c main.c
上面的工作很好,给了我一个输出文件,但我怀疑这是进行编译的有效方法还是其他方法

我的问题在《守则》的评论中提到,如下所示

I have tried like 
do(cd $i;make;)//this showing error
also do(cd $$i;)make;//this also sowing error
,请给我一些想法。 我翻阅了一些书,发现$$表示当前的进程Id,但是在I之前,这个有什么用呢

另外,使用
()
,将两个命令[
cd
make
]括起来有什么意义


请回答我的疑问。

make
在规则的操作中展开所有make变量(如
$(CFLAGS)
$@
),然后使用shell执行它。而
$$
扩展为
$
。因此,shell在其
for
循环中看到
$i
。如果您只在规则中写入了
$i
,那么在调用shell之前,它将被
$(i)
扩展为空(因为make没有任何绑定
i
make变量)

使用,如
remake-x
来理解正在发生的事情

顺便说一句,我相信你的
Makefile
是错误的:
(cd$$I;make)
应该是
$(make)-C$$I
(因为GNU make进程非常特别,
$(make)
,例如,当您启动一个
make-j2

1:正如@BasileStarynkevitch所说,make扩展了这一点:

for i in $(DIR); do (cd $$i; make); done
为此:

for i in h1 h2; do (cd $i; make); done
并将其传递给外壳

2:括号不是必需的。这同样有效:

for i in $(DIR); do cd $$i; make; done
[正如维吉尔指出的那样,我在这一点上错了。]

3:正如@BasileStarynkevitch所说,
$(MAKE)-C$$i
cd$$i更好;制作

4:您可以完全不使用循环(从而正确处理依赖项):

5:您完全可以不使用递归:

h1/h1.o: h1/h1.c
    gcc -c h1/h1.c -o h1/h1.o

h2/h2.o: h2/h2.c
    gcc -c h2/h2.c -o h2/h2.o
5:您可以使用以下方法使规则更清晰:

h1/h1.o: h1/h1.c
    gcc -c $< -o $@

h2/h2.o: h2/h2.c
    gcc -c $< -o $@

main.out:main.o h1/h2.o h1/h2.o
    gcc -o $@ $^
h1/h1.o:h1/h1.c
gcc-c$<-o$@
h2/h2.o:h2/h2.c
gcc-c$<-o$@
main.out:main.oh1/h2.oh1/h2.o
gcc-o$@$^

您可以进一步简化,但对于这样一个简单的makefile,它是不值得的。

我不同意您的看法。使用括号,
(cd$i;make)
在子shell中执行(这特别意味着
cd
不会影响主shell)。如果省略它们,这将由主shell直接执行,以便在第一步结束时停留在
h1
中(当然这不会发生在
$(MAKE)-C h1
中,这是使用它的另一个原因)。@Virgile:你完全正确,谢谢。我应该更仔细地测试。