Setting environment variables in pre-build event and using in compilation step

时光毁灭记忆、已成空白 提交于 2019-11-30 11:47:48

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.

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

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

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