Problem: VC++ 2010 SP1 project reference fails when .NET 4.5 installed

Applies to: Visual Studio Professional 2010Visual Studio Premium 2010Visual Studio Ultimate 2010

Symptoms


  • Project to project (P2P) references to native VC++ projects are shown in the UI as unresolved (on the “Framework and References” property page). This is indicated by a red exclamation point icon next to the reference name as well as a missing Full Path property.
  • Users that programmatically queue the references (via DTE) will get an empty result or exception.
  • During execution of unit test projects (pressing F5), you get the exception: Exception of type ‘System.Exception’ was thrown

Cause


There was a change in Microsoft.Common.targets between .NET 4.0 and .NET 4.5 that changes the behavior of project to project (P2P) references in Visual Studio 2010. This issue can be caused by any combination of Visual Studio 2010 SP1 and .NET 4.5.

Resolution


Create a text file named "Microsoft.CppBuildEx.targets" that contains the following text:

<!--
***********************************************************************************************
Microsoft.CppBuildEx.targets
WARNING: DO NOT MODIFY this file unless you are knowledgeable about MSBuild and have
created a backup copy. Incorrect changes to this file will make it
impossible to load or build your projects from the command-line or the IDE.
This file modifies the steps/targets required to build Visual C++ projects.
Copyright (C) Microsoft Corporation. All rights reserved.
***********************************************************************************************
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<!-- Overrides existing "SetToGetNativeTargetPath" to set 'DesignTimeBuild_GetNativeTargetPath' flag for all C++ projects reference.
SetToGetNativeTargetPath is only called during design time build. -->
<Target Name="SetToGetNativeTargetPath" >
<ItemGroup>
<ProjectReference>
<AdditionalProperties Condition="'%(Extension)' == '.vcxproj'">DesignTimeBuild_GetNativeTargetPath=true</AdditionalProperties>
</ProjectReference>
</ItemGroup>
</Target>
<!-- Overrides existing "GetTargetPath" to return TargetPath if the project is managed or
returns TargetPath if DesignTimeBuild_GetNativeTargetPath is true. -->
<Target Name="GetTargetPath" DependsOnTargets="_DetermineManagedStateFromCL" Returns="@(ManagedTargetPath)" >
<ItemGroup>
<ManagedTargetPath Include="$(TargetPath)" Condition="'$(ManagedAssembly)' == 'true' or '$(DesignTimeBuild_GetNativeTargetPath)' == 'true'" />
</ItemGroup>
</Target>
</Project>


Add the Microsoft.CppBuildEx.targets file to each of these folders (create the folders if needed):

32-bit (x86) Windows:
%ProgramFiles%\MSBuild\Microsoft.Cpp\v4.0\Platforms\Itanium\ImportAfter
%ProgramFiles%\MSBuild\Microsoft.Cpp\v4.0\Platforms\Win32\ImportAfter
%ProgramFiles%\MSBuild\Microsoft.Cpp\v4.0\Platforms\x64\ImportAfter

64-bit (x64) Windows:
%ProgramFiles(x86)%\MSBuild\Microsoft.Cpp\v4.0\Platforms\Itanium\ImportAfter
%ProgramFiles(x86)%\MSBuild\Microsoft.Cpp\v4.0\Platforms\Win32\ImportAfter
%ProgramFiles(x86)%\MSBuild\Microsoft.Cpp\v4.0\Platforms\x64\ImportAfter