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

It's 11 years later than when this question was originally asked. I am using VS 2019

if in the event you want to assign variables in your event like....

set ABC=123

Then you can't use $(ABC) as the $(ABC) is processed before it is handed to the command line to run.

You must use %ABC% as used by the command line. It doesnt know what $(ABC) is as it is only understood by visual studio.

To Further complicate things visual studio event editor uses % as an escape char. Ive noticed things starting %D are bad, %K, %Z and %K are good.

Apparently you can use %25 as the escape for %.

%DESTDIR% doesnt as the escaping garbles it - so changing it to %25DESDIR%25 fixes it.


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

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.