Macros/Environment variable in .sln and .vcproj files for Visual studio

Victor Ronin picture Victor Ronin · May 8, 2009 · Viewed 44.5k times · Source

I have two similar problems:

a) I have a solution which includes several projects and I want to be able easily switch project location by setting some environment variable/macro. As example this project can be located in \SolutionDir\Dir1\ or \SolutionDir\Dir2\ So, I want to specify that it should be located in \SolutionDir\$(Var) and just set the variable.

Is there any build in Visual Studio way to do it?

I know currently only two solutions - edit .sln file manual/programmatically to find this project and set correct path.

I wasn't able to use environment variable in .sln file.

b) I have a project which includes resources (.rc and .h) files. I want to be able to set their location through other environment variable or macro.

Something like \ProjectDir\$(Var2)\resource.rc

I found some promising info on property sheets, but Visual studio doesn't expand macros when I am using them in File tag in the .vcproj.

Thank you for any ideas how to solve this problem.

Regards, Victor

Answer

Rom picture Rom · Jul 14, 2009

Just use the environment variable in the relevant field:

OutputDirectory="$(MyEnvVariableName)\Bin"

One trick is that you need to restart the Visual Studio IDE each time you change the variable.

There is an MSDN article precisely about this: How to: Use Environment Variables in a Build