本文介绍了在Mac OS X上的Makefile中设置PATH(但在Linux上有效)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在Linux上的Makefile中设置PATH,但不能在Mac OS X上设置.使用OS X,可以设置PATH,但不会使用它.这是一个演示:

I'm able to set PATH in a Makefile on Linux but not Mac OS X. With OS X, the PATH gets set but doesn't get used. Here's a demonstration:

在具有bash 4.1.2(1)-release和GNU Make 3.81的CentOS 6上

On CentOS 6 with bash 4.1.2(1)-release and GNU Make 3.81

$ pwd
/tmp/experiment
$ find * -type f -ls
132133    4 -rw-rw-r--   1 tlim tlim      113 Aug 26 11:01 Makefile
132132    4 -rwxrwxr-x   1 tlim tlim       28 Aug 26 10:59 bin/myprog
$ cat Makefile 
export PATH := $(shell /bin/pwd)/bin:$(PATH)

what:
    @echo PATH=$$PATH
    @echo PATH=$(PATH)
    which myprog
    myprog
$ cat bin/myprog 
#!/bin/bash
echo HERE I AM.
$ make what
PATH=/tmp/experiment/bin:/usr/stack/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin
PATH=/tmp/experiment/bin:/usr/stack/bin:/usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin
which myprog
/tmp/experiment/bin/myprog
myprog
HERE I AM.

以下是在具有GNU Make 3.81的Mac OS X 10.9.4上运行的完全相同的文件:

Here's the exact same files run on a Mac OS X 10.9.4 with GNU Make 3.81:

$ pwd
/tmp/experiment
$ find * -type f -ls
51838886        8 -rw-rw-r--    1 tal              wheel                 113 Aug 26 07:01 Makefile
51838887        8 -rwxrwxr-x    1 tal              wheel                  28 Aug 26 06:59 bin/myprog
$ cat Makefile 
export PATH := $(shell /bin/pwd)/bin:$(PATH)

what:
    @echo PATH=$$PATH
    @echo PATH=$(PATH)
    which myprog
    myprog
$ cat bin/myprog 
#!/bin/bash
echo HERE I AM.
$ make what
PATH=/tmp/experiment/bin:/Users/tal/bin:/opt/local/sbin:/opt/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin
PATH=/tmp/experiment/bin:/Users/tal/bin:/opt/local/sbin:/opt/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin
which myprog
/tmp/experiment/bin/myprog
myprog
make: myprog: No such file or directory
make: *** [what] Error 1

如您所见,正在设置PATH并将其导出到环境中. "echo $ PATH"和哪个myprog"都表明已设置路径.但是,在运行"myprog"时,shell无法找到它.

As you can see, the PATH is getting set and exported into the environment. Both "echo $PATH" and "which myprog" show that the path is set. However, when running "myprog" the shell can't find it.

更新:如果我将这两行添加到Makefile的what配方中:

Update: If I add these two lines to the what recipe in the Makefile:

@echo SHELL=$$SHELL
@echo SHELL=$(SHELL)

Linux和Mac输出:

Both Linux and Mac output:

SHELL=/bin/bash
SHELL=/bin/sh

在运行make之前,环境中的SHELL均等于/bin/bash.这使我认为在Makefile中设置SHELL只是魔术,这是GNU Make手册说始终设置SHELL的众多原因之一.

Both have SHELL equal to /bin/bash in the environment before running make. This makes me think that having SHELL set in Makefile is just magic and is one of the many reason that The GNU Make Manual says to always set SHELL.

推荐答案

事实证明PATH变量是特殊变量,除非外壳"也已设置.换句话说,如果我将这一行添加到Makefile中,则可以在Mac和Linux上运行:

It turns out the PATH variable is special and isn't exported unless "SHELL" is also set. In other words, if I add this one line to the Makefile it works on both Mac and Linux:

SHELL=/bin/bash

这当然使我想知道Mac为什么与众不同.希望通过在此处记录此内容,其他看到此问题的人员也将从中受益.另外,我想知道Mac为何与众不同,如果确实如此.

This, of course, makes me wonder why the Mac is different. Hopefully by documenting this here others that see this problem will also benefit. Plus, I'd like to know why the Mac is different, if it really is.

这篇关于在Mac OS X上的Makefile中设置PATH(但在Linux上有效)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 11:24