[ACCEPTED]-Macros/Environment variable in .sln and .vcproj files for Visual studio-solution

Accepted answer
Score: 23

Just use the environment variable in the 4 relevant field:


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

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

Score: 21

I think I have the same goal than you: I 12 want to use environment variables to locate 11 some projects in a solution file (.sln) and 10 to use some environment variables to locate 9 some files within my projects.

I found a 8 way to do that and it works fine for me 7 (with Visual Studio 2005): - edit the .sln 6 file with a text editor and use environment 5 variables with the following syntax %MyEnvironmentVariable% - edit 4 the .vcproj files and replace the path to 3 the desired files with some variables, with 2 the following syntax $(MyEnvironmentVariable).

Hope 1 it helps... Cyrille

Score: 2

The best way to achieve what you describe 12 in b) is to use property sheets. Check out 11 also this very similar question.

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

I am not sure what 7 version of VS you use. VS2008 lets you define 6 for example an include directory like this: "$(OpenCVInclude)\cxcore\include". I 5 use it all the time. OpenCVInclude is a 4 macro defined in a property sheet.

As for 3 question a), I think there is no "clean" way 2 to do what you want. As an alternative you 1 could the configuration manager:

  • Include all the projects in the solution.
  • Name the project differently, for example based on the OEM.
  • For each project define release and debug configurations in the solution
  • In "Build->Configuration Manager" You can check or uncheck the "Build" column for each configuration. Check "build" for the relevant project.
Score: 1

I am not sure if you are building just C++ projects 21 or if you are also building C#\VB projects, but 20 one of the great things about Visual Studio 19 is all of the projects are really just MSBuild 18 projects. If you edit a project in a text 17 editor you will see that at the end of the 16 project it imports a .targets file. If 15 you track down and find follow the imports 14 you will find that almost all of the VS 13 projects import Microsoft.Common.Targets. Microsoft.Common.Targets 12 imports Custom.Before.Microsoft.Common.Targets. Using 11 this import you can import your own targets 10 file with your own custom actions.

I for 9 example have a target file that has a common 8 property defined across all projects in 7 a solution and a custom post build event 6 that processes at the end of each project 5 building.

Using this extension method and 4 by creating custom configurations in the 3 solution besides just the standard release\debug, you 2 should be able to create as complex of a 1 build configuration as you need.

Score: 0

(a) Cyrille gives a decent solution to what 18 you asked for. Another way is to keep your 17 changeable settings in one common place. Note 16 that this won't work for non-msbuild files 15 like *.sln and *.vcproj (until VS 2010).

Project 14 files: ... $(ChangeableDir)\foo.cs

Common.targets: ChangeThis ...

However, I 13 don't think this is a great way to do things. If 12 the differences between what you're building 11 are large pieces of functionality, you should 10 consider creating a branch in your source 9 control system. OTOH, if the differences 8 are minor -- eg hardcoded strings -- then 7 this leads to your second question...

(b) The 6 kind of resource management you describe 5 is essentially the same problem faced by 4 people localizing their project into different 3 languages. Luckily, direct support is built 2 into Visual Studio since 2005. Check out 1 previous questions like: Localization in Visual Studio 2008

Score: 0

One solution is to set some enviroment variables 2 just before starting VS. In our projects 1 we use a batch file which looks like this:

SET ROOT=D:/root
SET COMPILER_ROOT=C:/Program Files (x86)/Microsoft Visual Studio 12.0/Common7/IDE
start "Microsoft Visual Studio 12.0" /D"%COMPILER_ROOT%\" "%COMPILER_ROOT%/devenv.exe" "%ROOT%\trunk\OurSolution.sln"

More Related questions