Linux 如何在 Makefile 中将 dir 添加到 $PATH 中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8941110/
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
How I could add dir to $PATH in Makefile?
提问by Szymon Lipiński
I want to write a Makefile which would run tests. Test are in a directory './tests' and executable files to be tested are in the directory './bin'.
我想编写一个可以运行测试的 Makefile。测试在目录“./tests”中,要测试的可执行文件在目录“./bin”中。
When I run the tests, they don't see the exec files, as the directory ./bin is not in the $PATH.
当我运行测试时,他们看不到 exec 文件,因为目录 ./bin 不在 $PATH 中。
When I do something like this:
当我做这样的事情时:
EXPORT PATH=bin:$PATH
make test
everything works. However I need to change the $PATH in the Makefile.
一切正常。但是我需要更改 Makefile 中的 $PATH。
Simple Makefile content:
简单的 Makefile 内容:
test all:
PATH=bin:${PATH}
@echo $(PATH)
x
It prints the path correctly, however it doesn't find the file x.
它正确打印路径,但是它没有找到文件 x。
When I do this manually:
当我手动执行此操作时:
$ export PATH=bin:$PATH
$ x
everything is OK then.
一切正常。
How could I change the $PATH in the Makefile?
如何更改 Makefile 中的 $PATH?
采纳答案by Eldar Abusalimov
Did you try export
directiveof Make itself (assuming that you use GNU Make)?
您是否尝试过 Make 本身的export
指令(假设您使用 GNU Make)?
export PATH := bin:$(PATH)
test all:
x
Also, there is a bug in you example:
此外,您的示例中有一个错误:
test all:
PATH=bin:${PATH}
@echo $(PATH)
x
First, the value being echo
ed is an expansion of PATH
variable performed by Make, not the shell. If it prints the expected value then, I guess, you've set PATH
variable somewhere earlier in your Makefile, or in a shell that invoked Make. To prevent such behavior you should escape dollars:
首先,被echo
ed的值是PATH
由 Make 执行的变量的扩展,而不是 shell。如果它打印出预期值,那么我猜你已经PATH
在 Makefile 的某个地方或在调用 Make 的 shell 中的某个地方设置了变量。为了防止这种行为,你应该逃避美元:
test all:
PATH=bin:$$PATH
@echo $$PATH
x
Second, in any case this won't work because Make executes each line of the recipe in a separate shell. This can be changed by writing the recipe in a single line:
其次,无论如何这都行不通,因为 Make 在单独的 shell 中执行配方的每一行。这可以通过在一行中编写配方来更改:
test all:
export PATH=bin:$$PATH; echo $$PATH; x
回答by karnhick
To set the PATH
variable, within the Makefile only, use something like:
要设置PATH
变量,仅在 Makefile 中,使用类似的内容:
PATH := $(PATH):/my/dir
test:
@echo my new PATH = $(PATH)
回答by Richard Pennington
What I usually do is supply the path to the executable explicitly:
我通常做的是明确提供可执行文件的路径:
EXE=./bin/
...
test all:
$(EXE)x
I also use this technique to run non-native binaries under an emulator like QEMU if I'm cross compiling:
如果我进行交叉编译,我还使用这种技术在 QEMU 等模拟器下运行非本地二进制文件:
EXE = qemu-mips ./bin/
If make is using the sh shell, this should work:
如果 make 正在使用 sh shell,这应该可以工作:
test all:
PATH=bin:$PATH x
回答by underspecified
Path changes appear to be persistent if you set the SHELL variable in your makefile first:
如果您首先在 makefile 中设置 SHELL 变量,则路径更改似乎是持久的:
SHELL := /bin/bash
PATH := bin:$(PATH)
test all:
x
I don't know if this is desired behavior or not.
我不知道这是否是理想的行为。
回答by kenorb
By design make
parser executes lines in a separate shell invocations, that's why changing variable (e.g. PATH
) in one line, the change may not be applied for the next lines (see this post).
根据设计,make
解析器在单独的 shell 调用中执行行,这就是为什么PATH
在一行中更改变量(例如),更改可能不会应用于下一行(请参阅此帖子)。
One way to workaround this problem, is to convert multiple commands into a single line (separated by ;
), or use One Shellspecial target (.ONESHELL
, as of GNU Make 3.82).
解决此问题的一种方法是将多个命令转换为一行(以 分隔;
),或使用One Shell特殊目标(.ONESHELL
,从 GNU Make 3.82 开始)。
Alternatively you can provide PATH
variable at the time when shell is invoked. For example:
或者,您可以PATH
在调用 shell 时提供变量。例如:
PATH := $(PATH):$(PWD)/bin:/my/other/path
SHELL := env PATH=$(PATH) /bin/bash