首页 > 解决方案 > 如何在 GNU Make 中修改 PATH 并成功使用 $(shell ...) 执行命令?

问题描述

背景:我有一个定制的工具链,应该由 Makefile 使用。工具链被检出(通过 svn:externals)到项目的工作副本中。Makefile 必须适应PATH变量。否则无法找到工具链二进制文件。在“.bashrc”或“.profile”中调整“PATH”是没有选择的,因为有几个项目使用不同版本的工具链。

请看这个最小的 Makefile,它展示了对交叉编译器 'cc' 的调用,它位于 '/home/edeviser/bin' 中的其他几个工具旁边:

export PATH:=/home/edeviser/bin:$(PATH)$
$(info Compiler used: $(shell which cc))

all:
    @echo "Compiler used: $(shell which cc)"
    @echo -n "Compiler used: "
    @which cc

调用后的输出make

Compiler used: /usr/bin/cc
Compiler used: /usr/bin/cc
Compiler used: /home/edeviser/bin/cc

我的期望是:

Compiler used: /home/edeviser/bin/cc
Compiler used: /home/edeviser/bin/cc
Compiler used: /home/edeviser/bin/cc

如何在 GNU Make 中修改 PATH 并成功使用 $(shell ...) 执行命令?

标签: shellmakefilepathgnu-make

解决方案


您必须编写这样的shell调用:

$(info Compiler used: $(shell PATH='$(PATH)' which cc))

在shell函数中设置PATH。


推荐阅读