Visual Studio: custom project variables

I created a property style sheet, to specify which Python (and SCons), now in my vcxproj, I can include the Python include dir by adding $(PythonIncludeDir) to the additional includes property.

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <ImportGroup Label="PropertySheets" />
    <PropertyGroup Label="UserMacros">
        <PythonDir>C:\Python27_64</PythonDir>
        <PythonExe>$(PythonDir)\python.exe</PythonExe>
        <PythonIncludeDir>$(PythonDir)\include</PythonIncludeDir>
        <PythonLibDir>$(PythonDir)\libs</PythonLibDir>
        <SCons>$(PythonExe) $(PythonDir)\Scripts\scons.py</SCons>
    </PropertyGroup>
    <ItemDefinitionGroup />
    <ItemGroup>
        <BuildMacro Include="PythonDir">
            <Value>$(PythonDir)</Value>
            <EnvironmentVariable>true</EnvironmentVariable>
        </BuildMacro>
        <BuildMacro Include="PythonExe">
            <Value>$(PythonExe)</Value>
            <EnvironmentVariable>true</EnvironmentVariable>
        </BuildMacro>
        <BuildMacro Include="PythonIncludeDir">
            <Value>$(PythonIncludeDir)</Value>
            <EnvironmentVariable>true</EnvironmentVariable>
        </BuildMacro>
        <BuildMacro Include="PythonLibDir">
            <Value>$(PythonLibDir)</Value>
            <EnvironmentVariable>true</EnvironmentVariable>
        </BuildMacro>
        <BuildMacro Include="SCons">
            <Value>$(SCons)</Value>
            <EnvironmentVariable>true</EnvironmentVariable>
        </BuildMacro>
    </ItemGroup>
</Project>

You can follow any solution as described below.

  1. Set the environment variable in a batch file. Create a batch file and insert code like the following:

    set MY_INCLUDES_DIR=D:\MyIncludes call "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Tools\vsvars32.bat" "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe" D:\MySolution.sln

Then start the Visual Studio solution by double-clicking this file. An advantage of this approach is that all projects can use the same environment variable.

  1. Define an xml tag in a Visual Studio project file within a PropertyGroup. One preexisting PropertyGroup that you can use for this purpose is the one with the label "UserMacros", which is empty by default.
<PropertyGroup Label="UserMacros">
    <!-- Add the following line. -->
    <MY_INCLUDES_DIR>D:\MyIncludes\</MY_INCLUDES_DIR>
</PropertyGroup>
  1. Create a system environment variable named MY_INCLUDES_DIR.

  2. Go to View(menubar) -> Property Manager. Select project, select platform and configuration, right click on Microsoft.Cpp.x64.user then go to User Macros Tab and click on Add Macros.

Whether you choose option 1, 2, 3, or 4, you can then refer to $(MY_INCLUDES_DIR) in your Visual Studio project files.


I know this works for VS 2010:

If you have property sheets, there is a section called "User Macros" under the "Common Properties" tree. You can create your own $(MyNamedVariable) macro, and even define it in terms of other $(SomeExistingMacro). If you have not played with property sheets before, look for the "Property Manager" under the view menu, and it will allow you to create and edit them.

Any project configuration that inherits the property sheet will see your macro as if VS had defined it itself, ie it will show up in the list of macros. User of your project can just go to some base property sheet, and edit the MY_INCLUDES_DIR (to use your example) in a single place.

Hope this helps.

(How to create property sheets? See http://msdn.microsoft.com/en-us/library/5k4a0033.aspx. View->Other Windows->Property Manger)