makefile 可以更新调用环境吗?
是否可以从 makefile 更新环境? 我希望能够创建一个目标来为它们设置客户端环境变量。 像这样的事情:
AXIS2_HOME ?= /usr/local/axis2-1.4.1
JAVA_HOME ?= /usr/java/latest
CLASSPATH := foo foo
setenv:
export AXIS2_HOME
export JAVA_HOME
export CLASSPATH
这样客户端就可以简单地执行以下操作:
make setenv all
java MainClass
并让它工作,而无需他们自己设置 java 执行的类路径。
或者我是否想以错误的方式做到这一点,并且有更好的方法?
Is it possible to update the environment from a makefile? I want to be able to create a target to set the client environment variables for them. Something like this:
AXIS2_HOME ?= /usr/local/axis2-1.4.1
JAVA_HOME ?= /usr/java/latest
CLASSPATH := foo foo
setenv:
export AXIS2_HOME
export JAVA_HOME
export CLASSPATH
So that the client can simply do:
make setenv all
java MainClass
and have it work without them needing to set the classpath for the java execution themselves.
Or am I looking to do this the wrong way and there is a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,您不能通过这种方式更新调用过程中的环境。 一般来说,子进程不能修改父进程的环境。 一个值得注意的例外是 Windows 上从 cmd shell 运行时的批处理文件。 根据您展示的示例,我猜您不是在 Windows 上运行。
通常,您想要完成的任务是通过 shell 脚本完成的,该脚本设置环境,然后调用您想要的进程。 例如,您可以编写如下 go.sh 脚本:
使 go.sh 可执行,现在您可以将应用程序作为
./go.sh
运行。 如果您愿意,您也可以使脚本更加复杂——例如,您可能希望将“MainClass”作为脚本的参数,而不是对其进行硬编码。No, you can't update the environment in the calling process this way. In general, a subprocess cannot modify the environment of the parent process. One notable exception is batch files on Windows, when run from a cmd shell. Based on the example you show, I guess you are not running on Windows though.
Usually, what you're trying to accomplish is done with a shell script that sets up the environment and then invokes your intended process. For example, you might write a go.sh script like this:
Make go.sh executable and now you can run your app as
./go.sh
. You can make your script more elaborate too, if you like -- for example, you may want to make "MainClass" a parameter to the script rather than hard coding it.从你的问题来看,我假设你正在使用 bash shell。
您可以将变量定义放在 shell 脚本中,如下所示:
然后将脚本
source
到当前环境中,使用或 只是
在当前 shell 中执行脚本 (即没有子进程),因此脚本所做的任何环境更改都将持续存在。
From your question I am assuming you're using the bash shell.
You can place the variable definitions in a shell script, like so:
And then
source
the script into the current environment, withor just
That executes the script in the current shell (i.e. no child process), so any environment changes the script makes will persist.
简单的答案是肯定的,但是在您的代码中,您需要在 setenv: 指令中定义变量。 在 Makefile 的开头执行此操作使其成为 Makefile 的局部变量。 我会在文件顶部使用 LOCAL_...,然后在 setenv: 指令中使用 VAR=LOCAL_VAR 等进行设置...还要记住,您只需要使用 make setenv 来调用 makefile。 我真的会考虑在 bash 脚本中执行此操作,因为需要在 Makefile 之外创建变量。 在环境中生成变量后,您应该能够从 Makefile 中进行分配和导出。
The quick answer is yes, however in your code, you would need to define the variables in the setenv: directive. Doing it at the beginning of the Makefile makes it a local variable to the Makefile. I would use LOCAL_... at the top of the file then set it in the setenv: directive with VAR=LOCAL_VAR etc... Also remember that you will need to call the makefile with make setenv only. I would really look into doing this in a bash script as the variable needs to be created outside of the Makefile. Once the variable has been generated in the environment, you should be able to assign and export from the Makefile.