Visual Studio预生成事件和批处理集 [英] Visual Studio Pre build events and batch set

查看:269
本文介绍了Visual Studio预生成事件和批处理集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

批处理文件如下所示(它是在检测ATI Stream SDK或NVidia CUDA工具箱之前自动生成的):

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"

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

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:

严重错误C1083:无法打开包含文件:'CL/cl.hpp':没有此类文件或目录

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

我知道,如果我想从Visual Studio GUI中访问它们,可以将这些变量放入注册表中,但是我真的不愿意这样做.有没有办法让这些环境变量在预构建事件发生后仍然存在?我不能直接引用$(ATISTREAMSDKROOT),因为该项目必须能够同时为ATI Stream和NVidia Cuda构建.

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.

推荐答案

预构建事件是在其自身的外壳中执行的(VS生成cmd.exe进程),因此您对 set 的所有调用>仅在该实例本地. 既然您说您的批处理文件是预先生成的,那么就没有必要真正使用pre-build事件了吗?因为还有其他方法可以使VS访问这些变量:

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:

不是简单地启动VS,而是启动Shell,调用批处理文件,然后启动devenv.exe.或制作一个批处理文件来完成所有这一切:

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

另一个选项:代替生成批处理文件,而是生成包含变量的属性表,并让您的项目包含该属性表.这样,您无需求助于环境变量,而是使用"VS方式"来处理变量.通过在常规项目设置中设置继承的项目属性"或在vcproj文件的配置"部分中添加"InheritedPropertySheets = my.vsprops"来添加文件.属性表文件示例:

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>

这篇关于Visual Studio预生成事件和批处理集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆