windows 在预构建事件中设置环境变量并在编译步骤中使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/419979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting environment variables in pre-build event and using in compilation step
提问by Paul Stephenson
In Visual Studio 2003, I am trying to set an environment variable in the pre-build event that will then be used in the compilation step, but the value doesn't seem to be propagated. For example, if the pre-build event contains this (either directly or within a batch file):
在 Visual Studio 2003 中,我试图在预构建事件中设置一个环境变量,然后将在编译步骤中使用该变量,但该值似乎没有传播。例如,如果预构建事件包含此内容(直接或在批处理文件中):
set MY_LIB_VERSION=1.0.0
and AdditionalIncludeDirectories has this:
和 AdditionalIncludeDirectories 有这个:
c:\path\to\library\my_lib_v$(MY_LIB_VERSION)\include
then I would expect the compilation to work if the my_lib_v1.0.0
directory exists. But instead, I get
如果my_lib_v1.0.0
目录存在,那么我希望编译能够工作。但相反,我得到
c:\path\to\prog\my_prog.c(22) : fatal error C1083: Cannot open include file: 'my_lib.h' Project : warning PRJ0018 : The following environment variables were not found: $(MY_LIB_VERSION)
I deduce that the environment variable set in the pre-build event therefore isn't being propagated to the compilation step, but I may be missing something.
我推断在预构建事件中设置的环境变量因此没有传播到编译步骤,但我可能遗漏了一些东西。
How can I set the environment variable in the pre-build event and use it in the compilation step?
如何在预构建事件中设置环境变量并在编译步骤中使用它?
(Alternatively, any other sensible ways of defining a library version once and using it several times for AdditionalIncludeDirectories and AdditionalLibraryDirectories would do just as well.)
(或者,任何其他明智的方式定义一个库版本一次并为 AdditionalIncludeDirectories 和 AdditionalLibraryDirectories 多次使用它也可以。)
Update: I ended up solving our problem in a different way. We are using Subversion, and set up the svn:externals
property on a subdirectory of the project source called dependencies
, such that a checkout of the project would additionally check out <svn_path>\libraries\my_lib_v1.0.0
and call it dependencies\my_lib
in the working copy. Then the project settings could refer to dependencies\my_lib\include
and suchlike. Upgrading to version 1.0.1 of my_lib
is then simply a matter of editing the svn:externals
property -- the code and project settings did not need to change.
更新:我最终以不同的方式解决了我们的问题。我们正在使用 Subversion,并svn:externals
在名为 的项目源的子目录上设置属性,以便项目dependencies
的检出将额外检出<svn_path>\libraries\my_lib_v1.0.0
并dependencies\my_lib
在工作副本中调用它。然后项目设置可以参考之dependencies\my_lib\include
类的。升级到 1.0.1 版my_lib
只需简单地编辑svn:externals
属性——无需更改代码和项目设置。
采纳答案by Paul Hollingsworth
You might want to investigate this tool: http://workspacewhiz.com/SolutionBuildEnvironmentReadme.html
您可能想要调查此工具:http: //workspacewhiz.com/SolutionBuildEnvironmentReadme.html
We use it all the time to manage environment variables in our build environment.
我们一直使用它来管理构建环境中的环境变量。
回答by Timo Geusch
I must admit that I've never attempted to set environment variables in a pre-build step, and I can see why it wouldn't necessarily work (running a batch file would most likely trigger a separate process, whereas you'd want to manipulate the parent process's environment).
我必须承认,我从未尝试在预构建步骤中设置环境变量,我可以理解为什么它不一定有效(运行批处理文件很可能会触发一个单独的进程,而您想要操纵父进程的环境)。
A workaround I've been using, but which will only work when you can determine the necessary settings before starting Visual Studio, is to create a batch file that sets the necessary environment variables and then kicks off Visual Studio with the appropriate solution file. I've reproduced the skeleton of this batch file below:
我一直在使用的一种解决方法是创建一个批处理文件,该文件设置必要的环境变量,然后使用适当的解决方案文件启动 Visual Studio,但只有在您可以在启动 Visual Studio 之前确定必要的设置时,它才会起作用。我在下面复制了这个批处理文件的骨架:
REM
REM Set up VS environment with defaults (this is for 2008) - need to do this first
REM
call "C:\Program Files\Microsoft Visual Studio 9.0\Common7\Tools\vsvars32.bat"
REM
REM Set the environment variables required by the project
REM
set BOOST_BASE=C:\Boost\include\boost-1_35
REM
REM If you need to manipulate the path, do it here
REM
REM
REM Finally, start VS with the appropriate solution file
REM
devenv MyProjectWithBoost.sln
回答by Sam Meldrum
Environment variables which are set using the SET command are temporary and only last for the lifetime of the process in which they are set. They immediately expire when the process expires - and can't be seen by other processes.
使用 SET 命令设置的环境变量是临时的,仅在设置它们的进程的生命周期内有效。它们在进程到期时立即到期 - 其他进程无法看到。
A Visual Studio pre-build event is a separate process. Once that process expires that environment variable ceases to be.
Visual Studio 预生成事件是一个单独的过程。一旦该过程到期,该环境变量就不再存在。
Are you sure that environment variables are what you want? Could you do this by setting a value in a text file held on a central network location?
您确定环境变量是您想要的吗?您可以通过在保存在中央网络位置的文本文件中设置一个值来做到这一点吗?
EDIT: If you really want to persistently change environment variables in Windows you can do it but it will involve calling into some Windows APIs rather than just calling SET. E.g. http://code.activestate.com/recipes/416087/
编辑:如果您真的想在 Windows 中持续更改环境变量,您可以这样做,但这将涉及调用某些 Windows API 而不仅仅是调用 SET。例如http://code.activestate.com/recipes/416087/
Try googling environment variable windows persisting
尝试谷歌搜索环境变量窗口持久化
回答by AnthonyLambert
It's 11 years laster than when this question was originally asked. I am using VS 2019
这比最初提出这个问题的时间晚了 11 年。我正在使用 VS 2019
if in the event you want to assign variables in your event like....
如果你想在你的事件中分配变量,比如......
set ABC=123
设置 ABC=123
Then you can't use $(ABC) as the $(ABC) is processed before it is handed to the command line to run.
然后你不能使用 $(ABC) 因为 $(ABC) 在它被传递到命令行运行之前被处理。
You must use %ABC% as used by the command line. It doesnt know what $(ABC) is as it is only understood by visual studio.
您必须使用命令行使用的 %ABC%。它不知道 $(ABC) 是什么,因为它只能被 Visual Studio 理解。
To Further complicate things visual studio event editor uses % as an escape char. Ive noticed things starting %D are bad, %K, %Z and %K are good.
为了进一步复杂化,visual studio 事件编辑器使用 % 作为转义字符。我注意到从 %D 开始的事情很糟糕,%K、%Z 和 %K 是好的。
Apparently you can use %25 as the escape for %.
显然,您可以使用 %25 作为 % 的转义符。
%DESTDIR% doesnt as the escaping garbles it - so changing it to %25DESDIR%25 fixes it.
%DESTDIR% 不会因为转义乱码 - 因此将其更改为 %25DESDIR%25 修复它。