pesche
pesche

Reputation: 3134

How can I provide a default value for a variable in VS2010 project?

I'm using environment variables in my VS2010 projects; this works fine.

But I'd like to have default values that are used whenever the environment variable doesn't exist. Is this possible?

Edit/Solved: my property sheet looks like this (THIRDPARTY_ROOT can be overridden by setting it in the environment):

<?xml version="1.0" encoding="utf-8"?>
<!--
 * Property sheet for 3rd party libraries
 *
 * - The BOOST_VER variable should be set to the same value as in common.mk
 * - The THIRDPARTY_ROOT variable can be overridden by an environment variable
 *   with the same name.
 *
 -->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ImportGroup Label="PropertySheets" />
  <PropertyGroup Label="UserMacros">
    <BOOST_VER>boost_1_48_0</BOOST_VER>
    <THIRDPARTY_ROOT Condition="'$(THIRDPARTY_ROOT)' == '' ">C:\local\3rdparty</THIRDPARTY_ROOT>
  </PropertyGroup>
  <PropertyGroup />
  <ItemDefinitionGroup>
    <ClCompile>
      <AdditionalIncludeDirectories>$(THIRDPARTY_ROOT)\$(BOOST_VER)</AdditionalIncludeDirectories>
    </ClCompile>
    <Link />
  </ItemDefinitionGroup>
  <ItemGroup>
    <BuildMacro Include="BOOST_VER">
      <Value>$(BOOST_VER)</Value>
    </BuildMacro>
    <BuildMacro Include="THIRDPARTY_ROOT">
      <Value>$(THIRDPARTY_ROOT)</Value>
    </BuildMacro>
  </ItemGroup>
</Project>

Upvotes: 2

Views: 2285

Answers (1)

Cody Gray
Cody Gray

Reputation: 244782

One possible solution is to provide default values in your project file.

You can do this by using a Condition attribute when defining this variable in your project file to indicate that it should be used only if the property has no value.

For example, if ToolsPath is your environment variable, you could add this code to your project file:

<ToolsPath Condition="'$(ToolsPath)' == '' ">
    C:\Tools
</ToolsPath>

(MSDN Reference)

Upvotes: 3

Related Questions