在 Visual Studio C++project 中设置环境变量 [英] Setting Environment variable in Visual studio C++project

查看:119
本文介绍了在 Visual Studio C++project 中设置环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到之前发布了类似的问题,但我需要稍微不同的解决方案.
我有一个 VS2012 C++ 项目 A.vcxproj,它有 debug1 配置(平台 x64),它构建了相应的 .exe.它还使用来自必须添加到环境变量的路径的其他 VS2012 C 项目 B.vcxproj 的 dll.A.vcxproj.user 文件有以下文本

I realize that similar questions were posted earlier but I need slightly different solution.
I have VS2012 C++ project A.vcxproj,that has debug1 configuration( platform x64) , and it builds corresponding .exe. It also uses a dll from other VS2012 C project B.vcxproj from a path that must be added to the environment variables. A.vcxproj.user file has following text

<LocalDebuggerEnvironment>PATH=%PATH%;C:\Program Files\libsndfile\bin;..\..\lib\simulink\;$(LocalDebuggerEnvironment)  </LocalDebuggerEnvironment>

我需要将此设置自动添加到具有以下约束的A"项目

I need to add this setting automatically to the "A"project with following constraints

  1. 我无法将用户文件导出为每个用户",因此无法上传到我们的 SCM 系统以便其他用户可以下载.
  2. 我尝试在主函数中添加代码,比如

  1. I cannot export user file as it as "per user", so cannot upload to our SCM system where other users can download it.
  2. I tried adding code in main function, something like

_putenv("PATH = ....\lib\simulink");
但这不起作用,因为在编译主文件之前,它需要从指定路径搜索dll,它不会.

_putenv("PATH = ....\lib\simulink");
but this does not work, as before the main file is compiled, it needs to search for the dll from specified path, which it dosn't .

谁能建议一个简单、便携的修复程序,我可以通过 SCM 将它与项目文件一起分发给所有用户.

Can anyone suggest a easy, portable fix , that i could distribute to all users through SCM, along with the project file.

我也试过以下:
--创建批处理文件setpath.bat,内容如下

I have also tried following:
--Created batch file setpath.bat with following content

@ECHO %PATH%  set PATH = %PATH%;C:\Program Files\libsndfile\bin;C:\dev\lib\simulink 

--添加到A.vcxproj settings->build event->Pre-build->Command line

-- added to A.vcxproj settings->build event->Pre-build->Command line

call  C:\setpath.bat

并且我在 vS op 窗口下没有看到添加的路径.VS 用户文件也没有得到更改,并且运行该项目会抱怨缺少 dll 错误.
--我试图在
中执行批处理文件A.vcxproj设置->自定义构建步骤->在运行"之前执行
还是没有结果.

and I don't see the added paths under vS op window. neither does the VS User file gets the change, and running the project complains for missing dll error.
--I tried to execute the batch file in
A.vcxproj settings-> Custom build step->Execute before "Run"
and still no result.

我想解决方案需要在 VS 项目运行"时将所需的路径添加到当前环境变量.

I guess the solution needs to add needed path to current environment variable for the time VS project is "run".

谢谢
塞迪

推荐答案

加入A.vcxproj settings-> Build Events ->Pre-Build event

added to A.vcxproj settings-> Build Events ->Pre-Build event

call setdllpath.bat

其中文件包含以下内容:

where the file contains the following:

@ECHO %PATH%
set COMSPEC = "%VCINSTALLDIR%\vcvarsall.bat" amd64
setx PATH "C:\Program Files\libsndfile\bin;..\..\lib\simulink" 
@ECHO %PATH%

因此,一旦我构建了项目,关闭 Visual Studio 并再次打开它,并在项目中运行文件,它就会正确地获取 dll.

So, once I build the Project, close Visual studio and open it again, and run the files wiithin project, it picks up dll correctly.

这篇关于在 Visual Studio C++project 中设置环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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