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

时间:2010-03-03 16:38:19

标签: bash shell makefile

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

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

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

print:
    @echo $(HEADER)

我得到了

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

如果我直接在控制台中运行此命令,并直接在我的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
....

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

有什么想法吗?

3 个答案:

答案 0 :(得分:70)

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

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

这里的问题是make会尝试将$f扩展为变量,并且因为它没有找到任何内容,所以它只是用“”替换它。这使得你的shell命令只有echo ile,但忠实地做了。

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

答案 1 :(得分:14)

为什么不简单地做

HEADER = $(shell find . -name '*.h')

答案 2 :(得分:7)

makefile tutorial建议使用wildcard获取目录中的文件列表。在您的情况下,它意味着:

HEADERS=$(wildcard *.h)