将makefile变量值分配给bash命令结果?

Gol*_*les 42 bash shell makefile

我正在尝试将此命令的输出(即在我的makefile中)分配给makefile HEADER var,如下面的代码行所示:

HEADER = $(shell for file in `find . -name *.h`;do echo $file; done)
Run Code Online (Sandbox Code Playgroud)

问题是,如果我使用以下命令在makefile中打印HEADER:

print:
    @echo $(HEADER)
Run Code Online (Sandbox Code Playgroud)

我明白了

ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile ile
Run Code Online (Sandbox Code Playgroud)

如果我直接在控制台中运行此命令,并直接在我的makefile所在的位置:

myaccount$ for file in `find . -name *.h`;do echo $file; done
./engine/helper/crypto/tomcrypt/headers/._tomcrypt_pk.h
./engine/helper/crypto/tomcrypt/headers/tomcrypt.h
./engine/helper/crypto/tomcrypt/headers/tomcrypt_argchk.h
./engine/helper/crypto/tomcrypt/headers/tomcrypt_cfg.h
./engine/helper/crypto/tomcrypt/headers/tomcrypt_cipher.h
./engine/helper/crypto/tomcrypt/headers/tomcrypt_custom.h
./engine/helper/crypto/tomcrypt/headers/tomcrypt_hash.h
./engine/helper/crypto/tomcrypt/headers/tomcrypt_mac.h
....
Run Code Online (Sandbox Code Playgroud)

所以我得到了所有的头文件.我这样做是为了避免在makefile中手动指定所有我的.h文件.

有任何想法吗 ?

e.J*_*mes 74

您需要$在shell命令中双重转义字符:

HEADER = $(shell for file in `find . -name *.h`;do echo $$file; done)
Run Code Online (Sandbox Code Playgroud)

这里的问题是make会尝试扩展$f为变量,因为它没有找到任何东西,它只是用""替换它.这使得你的shell命令什么都没有,但echo ile它忠实地做了.

添加$$tell make会$在该位置放置一个,这会导致shell命令完全按照您希望的方式查找.


Sor*_*gal 14

为什么不简单呢

HEADER = $(shell find . -name '*.h')
Run Code Online (Sandbox Code Playgroud)

  • 这是针对此特定问题的正确解决方法,但没有解释“为什么”原始命令失败(即需要用“$$”对“$”进行编码)。 (3认同)

BЈо*_*вић 7

生成文件教程建议使用wildcard来获得目录中的文件列表.在你的情况下,它意味着:

HEADERS=$(wildcard *.h)
Run Code Online (Sandbox Code Playgroud)

  • 这并不等同于问题中的代码.解析Makefile时会发生通配符替换,而例如,只有执行Makefile规则后才能执行shell命令.在测试Makefile生成的文件时,这会有所不同. (6认同)