将 makefile 变量值分配给 bash 命令结果?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2373081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Assign a makefile variable value to a bash command result?
提问by Goles
I'm trying to assign the output of this command ( that is in my makefile ) to the makefile HEADER var like in this following line of code:
我正在尝试将此命令的输出(即在我的 makefile 中)分配给 makefile HEADER var,如下面的代码行所示:
HEADER = $(shell for file in `find . -name *.h`;do echo $file; done)
The problem is that if I print HEADER in my makefile using:
问题是,如果我使用以下命令在我的 makefile 中打印 HEADER:
print:
@echo $(HEADER)
I get
我得到
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
And if I run this command directly in the console, and directly where my makefile is:
如果我直接在控制台中运行此命令,并且直接在我的 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
....
So I get all my header files. I'm doing this to avoid manually specifying all my .h files manually in my makefile.
所以我得到了我所有的头文件。我这样做是为了避免在我的 makefile 中手动指定我所有的 .h 文件。
Any ideas ?
有任何想法吗 ?
回答by e.James
You will need to double-escape the $
character within the shell command:
您需要$
在 shell 命令中对字符进行双重转义:
HEADER = $(shell for file in `find . -name *.h`;do echo $$file; done)
The problem here is that make will try to expand $f
as a variable, and since it doesn't find anything, it simply replaces it with "". That leaves your shell command with nothing but echo ile
, which it faithfully does.
这里的问题是 make 会尝试将其扩展$f
为一个变量,并且由于它没有找到任何东西,它只是简单地将其替换为 ""。这让你的 shell 命令只剩下echo ile
,它忠实地做到了。
Adding $$
tells make to place a single $
at that position, which results in the shell command looking exactly the way you want it to.
添加$$
告诉 make$
在该位置放置一个单曲,这会导致 shell 命令看起来完全符合您的要求。
回答by sorpigal
Why not simply do
为什么不简单地做
HEADER = $(shell find . -name '*.h')
回答by B?ови?
The makefile tutorialsuggested to use wildcard
to get list of files in a directory. In your case, it means this :
在生成文件教程建议使用wildcard
来获得目录中的文件列表。在您的情况下,这意味着:
HEADERS=$(wildcard *.h)