我正在使用environment variables in my VS2010 projects;这很好。
但是我希望在环境变量不存在时使用默认值。这可能吗?
编辑/解决:我的属性表看起来像这样(可以通过在环境中设置来覆盖THIRDPARTY_ROOT):
<?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>
答案 0 :(得分:3)
一种可能的解决方案是在项目文件中提供默认值。
您可以在项目文件中定义此变量时使用Condition
属性来执行此操作,以指示仅当属性没有值时才应使用该属性。
例如,如果ToolsPath
是您的环境变量,则可以将此代码添加到项目文件中:
<ToolsPath Condition="'$(ToolsPath)' == '' ">
C:\Tools
</ToolsPath>