Migrating a C++/CLI Project to Visual Studio 2010

You’ve probably read that Visual Studio 2010 supports multi-targeting: with the flick of a project property switch, the compiler can generate binaries for either the .NET 3.5 or .NET 4 platforms. And in fact, when you upgrade a Visual Studio 2008 solution to 2010, the upgrader will set the target framework to 3.5 so that your code compiles and runs exactly as before. (New projects, by default, target .NET 4.)

However, this is only true for C# and VB projects; C++/CLI projects will automatically be upgraded to .NET 4 and the new VC10 compiler. It is possible to change this, but there is no UI1; you have to edit the project file manually. And, most importantly, Visual Studio 2008 SP1 must be installed side-by- side for this to work2. The VC10 compiler has no support for .NET 3.5, so Visual Studio will invoke the VC9 compiler (included in VS2008) if you target the .NET 3.5 platform.

To change the target framework3:

  1. Open the solution in VS2010 and have it convert the project files to the new VCXPROJ format.
  2. Close the solution, or manually unload the C++ project file.
  3. Open the VCXPROJ project file in a text editor.
  4. Find the XML element <PropertyGroup Label="Globals">
  5. Find the <TargetFrameworkVersion> child element; if not present add it.
  6. Set the inner text of the element to the desired framework version (v2.0, v3.0, v3.5, or v4.0), e.g., <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
  7. Optional: Add <TargetFrameworkProfile>Client</TargetFrameworkProfile> if you want to target the client profile (or remove that element if it exists to target the full framework)4.
  8. Save the VCXPROJ file and close it.
  9. Reload the project/solution in Visual Studio 2010.

Notes:

  1. There is UI for native-only projects; see Native Multi-Targeting at the Visual C++ Team Blog.
  2. See C++ Property Pages documentation at MSDN.
  3. Thanks to Đonny who provided excellent instructions in community content added to a MSDN article.
  4. Thanks to Rick Brewster at the Paint.NET Blog for this tip.

Posted by Bradley Grainger on April 15, 2010