如何在Makefile中将dir添加到$ PATH? [英] How I could add dir to $PATH in Makefile?

查看:662
本文介绍了如何在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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆