Visual Studio 预构建事件和批处理集

发布于 2024-10-09 22:32:21 字数 682 浏览 6 评论 0原文

我正在尝试创建调用批处理文件,该文件在构建之前设置一堆环境变量。

批处理文件看起来像这样(它是预先自动生成的,用于检测 ATI Stream SDK 或 NVidia CUDA 工具包):

set OCL_LIBS_X86="%ATISTREAMSDKROOT%libs\x86"
set OCL_LIBS_X64="%ATISTREAMSDKROOT%libs\x86_64"
set OCL_INCLUDE="%ATISTREAMSDKROOT%include"

但是,构建的其余部分似乎无法访问这些变量,因此当我尝试在 C/C++>General>Additional include 目录中引用 $(OCL_INCLUDE) 时,它会首先向我发出警告未找到环境变量 $(OCL_INCLUDE),当我尝试包含 CL/cl.hpp 时,编译将失败并显示:

致命错误 C1083:无法打开包含文件:'CL/cl.hpp':没有这样的文件或目录

我知道如果我想从 Visual Studio GUI 访问这些变量,我可以将它们放入注册表中,但我真的更喜欢不要这样做。有没有办法让这些环境变量在预构建事件之后保留?我无法直接引用 $(ATISTREAMSDKROOT),因为该项目必须能够针对 ATI Stream 和 NVidia Cuda 进行构建。

I'm trying to create call a batch file which sets a bunch of environment variables prior to building.

The batch file looks something like this (it's automatically generated before-hand to detect ATI Stream SDK or NVidia CUDA toolkit):

set OCL_LIBS_X86="%ATISTREAMSDKROOT%libs\x86"
set OCL_LIBS_X64="%ATISTREAMSDKROOT%libs\x86_64"
set OCL_INCLUDE="%ATISTREAMSDKROOT%include"

However, the rest of the build doesn't seem to have access to these variables, so when I try to reference $(OCL_INCLUDE) in the C/C++>General>Additional include directories, it will first give me warning that environment variable $(OCL_INCLUDE) was not found, and when I try to include CL/cl.hpp the compile will fail with:

fatal error C1083: Cannot open include file: 'CL/cl.hpp': No such file or directory

I know that I could put these variables into the registry if I wanted to access them from the visual studio GUI, but I would really prefer not to do this. Is there a way to to get these environment variables to stick after the pre-build events? I can't reference $(ATISTREAMSDKROOT) directly because the project must be able to build for both ATI Stream and NVidia Cuda.

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

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

发布评论

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

评论(1

濫情▎り 2024-10-16 22:32:21

预构建事件在它自己的 shell 中执行(VS 生成 cmd.exe 进程),因此对 set 的所有调用都仅在该实例本地。
既然您说您的批处理文件是预先生成的,那么实际上没有必要使用预构建事件,是吗?因为还有其他方法可以让 VS 访问这些变量:

不是简单地启动 VS,而是启动 shell,调用批处理文件,然后启动 devenv.exe。或者创建一个批处理文件来完成所有这些操作:

set OCL_LIBS_X86="%ATISTREAMSDKROOT%libs\x86"
set OCL_LIBS_X64="%ATISTREAMSDKROOT%libs\x86_64"
set OCL_INCLUDE="%ATISTREAMSDKROOT%include"
%comspec% /k "%VCINSTALLDIR%\vcvarsall.bat" x86
devenv.exe

另一种选择:不生成批处理文件,而是生成包含变量的属性表,并让您的项目包含该属性表。这样你就不用求助于环境变量,它更像是使用变量的“VS 方式”。通过在常规项目设置中设置“Inherited Project Properties”或将“InheritedPropertySheets=my.vsprops”添加到 vcproj 文件的“配置”部分来添加文件。属性表文件示例:

<?xml version="1.0" encoding="Windows-1252"?>
<VisualStudioPropertySheet
ProjectType="Visual C++"
Version="9.00"
Name="toolkit_selector"
>
<UserMacro
    Name="OCL_LIBS_X86"
    Value="$(ATISTREAMSDKROOT)libs\x86"
/>
<UserMacro
    Name="OCL_INCLUDE"
    Value="$(ATISTREAMSDKROOT)include"
/>
</VisualStudioPropertySheet>

a pre-build event is executed in it's own shell (VS spawns a cmd.exe process), hence all your calls to set are local to that instance only.
Since you say your batchfile is pre-generated, there's no real need to use a pre-build event, is there? Because there are other ways to get VS to get access to these variables:

Instead of simply launching VS, launch a shell, call the batchfile, then launch devenv.exe. Or make a batch file to do all of this:

set OCL_LIBS_X86="%ATISTREAMSDKROOT%libs\x86"
set OCL_LIBS_X64="%ATISTREAMSDKROOT%libs\x86_64"
set OCL_INCLUDE="%ATISTREAMSDKROOT%include"
%comspec% /k "%VCINSTALLDIR%\vcvarsall.bat" x86
devenv.exe

Another option: instead of generating a batch file, generate a property sheet containing the variables and have your project(s) include the property sheet. This way you don't resort to environment variables, it's more 'the VS way' to work with variables. Add the file by setting 'Inherited Project Properties' in the general project setting, or adding 'InheritedPropertySheets=my.vsprops' to the Configuration section in your vcproj file. Example property sheet file:

<?xml version="1.0" encoding="Windows-1252"?>
<VisualStudioPropertySheet
ProjectType="Visual C++"
Version="9.00"
Name="toolkit_selector"
>
<UserMacro
    Name="OCL_LIBS_X86"
    Value="$(ATISTREAMSDKROOT)libs\x86"
/>
<UserMacro
    Name="OCL_INCLUDE"
    Value="$(ATISTREAMSDKROOT)include"
/>
</VisualStudioPropertySheet>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文