Setting Environment variable in Visual studio C++project

一个人想着一个人 提交于 2021-02-15 07:49:27

问题


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>

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

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

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

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 

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

call  C:\setpath.bat

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.

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

Thanks
sedy


回答1:


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%

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




回答2:


Contents in *.vcxproj.user, *.vcxproj.user or *.props use the same xml schema so can be easily exchanged or included.

First if usefull you can add UserMacros to define the path to your libraries. Like bellow for the following two variable

PYTHONHOME=$(USERPROFILE)\AppData\Local\Programs\Python\Python37
PYTHONPATH=$(PYTHONHOME)\DLLs;$(PYTHONHOME)\Lib;$(PYTHONHOME)\Lib\site-packages

Edit the .vcxproj adding inside <Project>:

  <Project .... >
  ...

  <PropertyGroup Label="UserMacros">
  <PYTHONHOME>$(USERPROFILE)\AppData\Local\Programs\Python\Python37</PYTHONHOME>
  <PYTHONPATH>$(PYTHONHOME)\DLLs;$(PYTHONHOME)\Lib;$(PYTHONHOME)\Lib\site-packages;</PYTHONPATH>      </PropertyGroup>

  ...
  </Project>

After you can add inside your build configuration, the following to set the $(Path) variable.

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
    <LocalDebuggerEnvironment>Path=$(Path);$(PYTHONHOME);$(PYTHONHOME)\DLLs;$(PYTHONHOME)\Lib;$(PYTHONHOME)\Lib\site-packages;$(PYTHONHOME)\Scripts;$(PYTHONHOME);</LocalDebuggerEnvironment>



来源:https://stackoverflow.com/questions/31551068/setting-environment-variable-in-visual-studio-cproject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!