2010-03-03 49 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生成的文件時,這會有所不同。 –