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

发布于 2024-07-11 12:53:05 字数 1154 浏览 13 评论 0原文

在 Visual Studio 2003 中,我尝试在预构建事件中设置一个环境变量,然后在编译步骤中使用该变量,但该值似乎没有传播。 例如,如果预构建事件包含此内容(直接或在批处理文件中):

set MY_LIB_VERSION=1.0.0

并且AdditionalIncludeDirectories 包含此内容:

c:\path\to\library\my_lib_v$(MY_LIB_VERSION)\include

那么如果 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)

预构建事件中设置的环境变量因此不会传播到编译步骤,但我可能遗漏了一些东西。

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

(或者,可以使用任何其他合理的方法来定义一次库版本并将其多次用于AdditionalIncludeDirectories和AdditionalLibraryDirectories也可以。)


更新:我最终以不同的方式解决了我们的问题。 我们使用 Subversion,并在名为 dependencies 的项目源子目录中设置 svn:externals 属性,这样签出项目时还会签出 \libraries\my_lib_v1.0.0 并在工作副本中将其命名为 dependencies\my_lib。 然后项目设置可以参考dependency\my_lib\include等。 升级到 my_lib 版本 1.0.1 只需编辑 svn:externals 属性即可 - 代码和项目设置不需要更改。

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):

set MY_LIB_VERSION=1.0.0

and AdditionalIncludeDirectories has this:

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

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.)


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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

酒绊 2024-07-18 12:53:05

比最初提出这个问题的时间晚了 11 年。 我正在使用 VS 2019

如果你想在你的事件中分配变量,比如......

设置 ABC=123

那么你不能使用 $(ABC) 因为 $(ABC) 在它之前被处理交给命令行来运行。

您必须使用命令行所使用的 %ABC%。 它不知道 $(ABC) 是什么,因为只有 Visual Studio 才能理解它。

为了使事情变得更加复杂,Visual Studio 事件编辑器使用 % 作为转义字符。 我注意到以 %D 开头的东西很糟糕,以 %K、%Z 和 %K 开头的东西很好。

显然你可以使用 %25 作为 % 的转义符。

%DESTDIR% 不会,因为转义会混淆它 - 因此将其更改为 %25DESDIR%25 可以修复它。

It's 11 years later than when this question was originally asked. I am using VS 2019

if in the event you want to assign variables in your event like....

set ABC=123

Then you can't use $(ABC) as the $(ABC) is processed before it is handed to the command line to run.

You must use %ABC% as used by the command line. It doesnt know what $(ABC) is as it is only understood by 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.

Apparently you can use %25 as the escape for %.

%DESTDIR% doesnt as the escaping garbles it - so changing it to %25DESDIR%25 fixes it.

沩ん囻菔务 2024-07-18 12:53:05

我必须承认,我从未尝试在预构建步骤中设置环境变量,并且我可以理解为什么它不一定有效(运行批处理文件很可能会触发一个单独的进程,而您想要操纵父进程的环境)。

我一直在使用的一种解决方法是创建一个批处理文件来设置必要的环境变量,然后使用适当的解决方案文件启动 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

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:

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
朦胧时间 2024-07-18 12:53:05

您可能想研究一下这个工具:http://workspacewhiz.com/SolutionBuildEnvironmentReadme.html

我们都使用它在我们的构建环境中管理环境变量的时间。

You might want to investigate this tool: http://workspacewhiz.com/SolutionBuildEnvironmentReadme.html

We use it all the time to manage environment variables in our build environment.

蓝色星空 2024-07-18 12:53:05

使用 SET 命令设置的环境变量是临时的,并且仅在设置它们的进程的生命周期内有效。 当进程过期时它们立即过期 - 并且不能被其他进程看到。

Visual Studio 预构建事件是一个单独的过程。 一旦该进程到期,该环境变量就不再存在。

您确定环境变量是您想要的吗? 您可以通过在中央网络位置保存的文本文件中设置一个值来做到这一点吗?

编辑:如果您确实想持续更改 Windows 中的环境变量,您可以这样做,但它将涉及调用一些 Windows API,而不仅仅是调用 SET。 例如http://code.activestate.com/recipes/416087/

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

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.

A Visual Studio pre-build event is a separate process. Once that process expires that environment variable ceases to be.

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/

Try googling environment variable windows persisting

爱*していゐ 2024-07-18 12:53:05

您提到了 Visual Studio 2003,虽然我提到的后面的内容仅适用于新的 SDK 样式项目文件,但至少从那时起,有关 MSBuild 的这篇简介就一直适用于 MSBuild。 的确; 我刚刚检查了MSBuild for VS 2003 的旧文档 确认这一点,以防我弄错了。

引用自 微软

每个 MSBuild 项目都有一个独立的环境块:它只能看到对其自己的块的读取和写入。 MSBuild 仅在初始化属性集合时、在评估或生成项目文件之前读取环境变量。 此后,环境属性是静态的,即每个生成的工具都以相同的名称和值开始。

现在,从 Visual Studio 2022 开始,如果您的项目使用新的 SDK 样式项目文件,您可以通过在项目文件中包含以下属性组来设置 Visual Studio 变量 $(MY_LIB_VERSION) 的值:

<PropertyGroup>
    <MY_LIB_VERSION>1.0.0</MY_LIB_VERSION>
</PropertyGroup>

You mention Visual Studio 2003, and while the later stuff that I mention only applies to the new SDK style project files, this blurb about MSBuild has been been true of MSBuild since at least then. Indeed; I just checked the legacy documentation for MSBuild for VS 2003 to confirm this in case I was mistaken.

Quoting from Microsoft:

Each MSBuild project has an isolated environment block: it only sees reads and writes to its own block. MSBuild only reads environment variables when it initializes the property collection, before the project file is evaluated or built. After that, environment properties are static, that is, each spawned tool starts with the same names and values.

Now days, from Visual Studio 2022 onward, if your project was using the new SDK style project files, you could set the value of the Visual Studio variable $(MY_LIB_VERSION) by including the following property group in your project file:

<PropertyGroup>
    <MY_LIB_VERSION>1.0.0</MY_LIB_VERSION>
</PropertyGroup>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文