In Visual Studio 2003, I am trying to set an environment variable in the pre-build event that will then be used in the compilation step, but the value doesn't seem to be propagated. For example, if the pre-build event contains this (either directly or within a batch file):
set MY_LIB_VERSION=1.0.0
and AdditionalIncludeDirectories has this:
c:\path\to\library\my_lib_v$(MY_LIB_VERSION)\include
then I would expect the compilation to work if the my_lib_v1.0.0
directory exists. But instead, I get
c:\path\to\prog\my_prog.c(22) : fatal error C1083: Cannot open include file: 'my_lib.h' Project : warning PRJ0018 : The following environment variables were not found: $(MY_LIB_VERSION)
I deduce that the environment variable set in the pre-build event therefore isn't being propagated to the compilation step, but I may be missing something.
How can I set the environment variable in the pre-build event and use it in the compilation step?
(Alternatively, any other sensible ways of defining a library version once and using it several times for AdditionalIncludeDirectories and AdditionalLibraryDirectories would do just as well.)
Update: I ended up solving our problem in a different way. We are using Subversion, and set up the svn:externals
property on a subdirectory of the project source called dependencies
, such that a checkout of the project would additionally check out <svn_path>\libraries\my_lib_v1.0.0
and call it dependencies\my_lib
in the working copy. Then the project settings could refer to dependencies\my_lib\include
and suchlike. Upgrading to version 1.0.1 of my_lib
is then simply a matter of editing the svn:externals
property -- the code and project settings did not need to change.
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