在预构建事件中设置环境变量并在编译步骤中使用

时间:2021-12-24 11:22:38

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,并在名为dependencies的项目源的子目录中设置svn:externals属性,这样项目的签出将额外检出 \ libraries \ my_lib_v1.0.0并将其称为依赖项\ my_lib in工作副本。然后项目设置可以引用依赖项\ my_lib \ include等。升级到my_lib的1.0.1版只需要编辑svn:externals属性 - 代码和项目设置不需要更改。

3 个解决方案

#1


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.

我们一直使用它来管理构建环境中的环境变量。

#2


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

#3


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

尝试谷歌搜索环境变量窗口持久化

#1


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.

我们一直使用它来管理构建环境中的环境变量。

#2


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

#3


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

尝试谷歌搜索环境变量窗口持久化