2010-03-03 52 views
33

我想在这下面的代码行此命令的输出(也就是在我的makefile)来生成文件HEADER VAR分配,如:将makefile变量值分配给bash命令结果?

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

的问题是,如果我打印的报头在我的makefile文件使用:

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 

如果我直接在控制台运行此命令,并直接在我的生成文件是:

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文件。

任何想法?

回答

61

您需要的shell命令内翻番,逃出$字符:

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

这里的问题是,使将努力扩大$f作为变量,因为它没有发现任何东西,它只是用“”替换它。这使得你的shell命令只有echo ile,它忠实地执行。

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

+0

很不错的响应:),我这样做是这种方式,因为在我的for循环其实我执行更复杂的东西,所以我只是贴小片段在这里。非常感谢@ e.James。 – Goles

14

为什么不能简单地做

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

makefile tutorial建议使用wildcard获得目录中的文件列表。根据你的情况,这意味着这样的:

HEADERS=$(wildcard *.h) 
+2

这并不等同于问题中的代码。在解析Makefile时会进行通配符替换,而例如,只有执行了Makefile规则后,shell命令才会发生。当测试由Makefile生成的文件时,这会有所不同。 –