2012-07-31 157 views
2

更改Makefile中的環境變量`PATH'在CLT中不會生效,並且可以使用我從原始源編譯的make util。設置內部makefile中的PATH變量對make不起作用3.81

簡單的Makefile

PATH := $(PATH):/opt/bin 

export PATH 

all: 
     @cscope --version 

我的測試

/tmp $ echo $PATH 
/usr/bin:/bin:/usr/sbin:/sbin 
/tmp $ ls /opt/bin/cscope 
/opt/bin/cscope 
/tmp $ which make 
/usr/bin/make 
/tmp $ make 
make: cscope: No such file or directory 
make: *** [all] Error 1 
/tmp $ ./_install/bin/make 
cscope: version 15.7a 
/tmp $ make --version 
GNU Make 3.81 
Copyright (C) 2006 Free Software Foundation, Inc. 
This is free software; see the source for copying conditions. 
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A 
PARTICULAR PURPOSE. 


This program built for i386-apple-darwin11.3.0 
/tmp $ ./_install/bin/make --version 
GNU Make 3.82 
Built for x86_64-apple-darwin12.0.0 
Copyright (C) 2010 Free Software Foundation, Inc. 
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> 
This is free software: you are free to change and redistribute it. 
There is NO WARRANTY, to the extent permitted by law. 

誰能幫助?

+0

是'cscope'的唯一工具你擔心,或者是其他的在'/ opt/bin /'中? – Beta 2012-07-31 17:57:25

+0

不,這只是一個簡單的測試util,不在系統'PATH'中。 – ShadowStar 2012-07-31 19:17:49

回答

1

您正在混淆make變量與shell變量。通過您的設置,調用的shell命令不會受到您設置的make變量PATH的影響。

您應該設置PATH變你的食譜裏,像

all: 
    @PATH=$(PATH):/opt/bin; cscope --version 

你必須既在同一行,因爲在配方中的每一行都會在另一個shell中運行,有效地失去了PATH設置,你剛剛做到了。或者,您可以通過在每行的末尾添加一個反斜槓\將其劃分到多行:

all: 
    @PATH=$(PATH):/opt/bin; \ 
    cscope --version 

更新

對不起,我錯過了你的makefile與make 3.82工作的重要性。我嘗試過兩種版本,並且3.813.82的確在這種情況下表現不同。

我沒有得到這個通過調用make如下與3.81的工作,雖然:

make SHELL="/bin/sh -c" 

make SHELL=/bin/bash 
+0

首先感謝您的回答。按照你的方式,我必須在每個目標的命令中添加「@PATH = $(PATH):/ opt/bin」。而從源頭上構建的util util沒有這個問題。 – ShadowStar 2012-07-31 17:46:57

+0

有問題的Makefile是一個簡單的例子,而不是真正使用的例子。 – ShadowStar 2012-07-31 18:08:20

+0

是的,它的工作原理。但環境變量中的'SHELL =/bin/bash'可以被'env'看到。 – ShadowStar 2012-07-31 18:55:33