如何在Makefile中将dir添加到$ PATH?

编程入门 行业动态 更新时间:2024-10-27 23:17:49
本文介绍了如何在Makefile中将dir添加到$ PATH?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想编写一个可以运行测试的Makefile.测试位于目录"./tests"中,而要测试的可执行文件位于目录"./bin"中.

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'.

运行测试时,他们看不到exec文件,因为目录./bin不在$ PATH中.

When I run the tests, they don't see the exec files, as the directory ./bin is not in the $PATH.

当我做这样的事情时:

EXPORT PATH=bin:$PATH make test

一切正常.但是我需要在Makefile中更改$ PATH.

everything works. However I need to change the $PATH in the Makefile.

简单的Makefile内容:

Simple Makefile content:

test all: PATH=bin:${PATH} @echo $(PATH) x

它可以正确打印路径,但是找不到文件x.

It prints the path correctly, however it doesn't find the file x.

当我手动执行此操作时:

When I do this manually:

$ export PATH=bin:$PATH $ x

那么一切都很好.

如何更改Makefile中的$ PATH?

How could I change the $PATH in the Makefile?

推荐答案

您尝试 Make本身的指令(假设您使用的是GNU Make)?

Did you try export directive of Make itself (assuming that you use 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

首先,echo ed的值是由Make(而不是外壳程序)执行的PATH变量的扩展.我想,如果它输出期望值,那么您已经在Makefile中或调用Make的shell中的某个位置设置了PATH变量.为了防止这种行为,您应该逃脱美元的侵害:

First, the value being echoed 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:

test all: PATH=bin:$$PATH @echo $$PATH x

第二,无论如何这都不起作用,因为Make在单独的外壳.可以通过在单行中编写配方来更改它:

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:

test all: export PATH=bin:$$PATH; echo $$PATH; x

更多推荐

如何在Makefile中将dir添加到$ PATH?

本文发布于:2023-05-31 10:09:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/389610.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中将   如何在   PATH   Makefile   dir

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!