3

I'm trying to migrate my .NET 2.0 C++/CLI project from VS2008 to VS2012. After the conversion, the project depends on 4.0. When I do the

<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>

the project depends on 2.0 and 4.0. I verify that by looking in the External Dependencies magic folder in Solution Explorer. It also gives me compiler warning when I reference the project in a 2.0 C# project.

Commenting out #includes reveals that the 4.0 dependency is pulled in by #using directives in Microsoft headers such as vcclr.h and atlbase.h.

I need these headers for the classes and functions they provide. How do I make the #using directives in them point to 2.0 version of mscorlib.dll instead of 4.0?

Ansis Māliņš
  • 1,684
  • 15
  • 35
  • What I want: CLI project dependent on .NET 2.0. What I have: CLI project dependent on .NET 2.0 *and* 4.0. – Ansis Māliņš Apr 03 '13 at 13:20
  • http://connect.microsoft.com/VisualStudio/feedback/details/741680/upgrade-from-vs2008-c-cli-changes-target-framework – Hans Passant Apr 03 '13 at 17:35
  • The newer C++/CLI compiler only supports .NET 4.x. (You can use the new editor with the old compiler, which is what all the related answers are explaining how to do) – Ben Voigt Apr 29 '13 at 19:43

2 Answers2

4

Changing the <TargetFrameworkVersion> isn't enough, the build tools still use the .NET 4 version that's installed on your machine. Something you can see by running ildasm.exe on the generated assembly. Double click the manifest to see:

// Metadata version: v4.0.30319
...
.assembly extern mscorlib
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .z\V.4..
  .hash = (DD 9E C9 8D BF 2A 2D C2 AA 2D C3 8B 51 CD 4C A6   // .....*-..-..Q.L.
           15 F0 22 F6 )                                     // ..".
  .ver 2:0:0:0
}
...
.assembly extern mscorlib as mscorlib_4
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .z\V.4..
  .hash = (47 6E C0 E3 BA CD CE B1 9A 4D 68 BE 29 75 61 6F   // Gn.......Mh.)uao
           BE 04 C6 BA ) 
  .ver 4:0:0:0
}

Note how both versions of mscorlib.dll ended up as a dependency. The problem you encountered.

The feedback article I linked recommended changing the toolset to "v90". This however doesn't work on my machine, even though I have VS2008 installed. The option doesn't show up in the combobox dropdown and when I force it I get this build error:

C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V110\Microsoft.Cpp.Platform.targets(43,5): error MSB8020: The builds tools for Visual Studio 2008 (Platform Toolset = 'v90') cannot be found. To build using the v90 build tools, either click the Project menu or right-click the solution, and then select "Update VC++ Projects...". Install Visual Studio 2008 to build using the Visual Studio 2008 build tools.

Following the advice given in the error message, I get this:

------ Update VC++ projects started -------
Updating project 'ConsoleApplication74'...
    Configuration 'Debug|Win32': changing Platform Toolset to 'v110' (was 'v90').
    Configuration 'Release|Win32': no update required. Platform Toolset is 'v110'.
    TargetFrameworkVersion = v4.5 (was v2.0)

Back to square one. I'd say that targeting .NET 2.0 in C++/CLI projects with VS2012 is a lost cause.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • 1
    Meanwhile, on my machine: Installed VS2008, set tools to v90, no 4.0 dependency, built successfully. The problem now is that VS2012 can't find the VS2008's standard headers and IntelliSense freaks out with bajillion errors like "can't find include ". – Ansis Māliņš Apr 30 '13 at 15:21
  • 1
    @Hans Passant you need to install 2008 + 2010 – Nahum Jul 09 '14 at 04:50
1

One way to do it is to open up the project file (.vcxproj) and change the

<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>

to

<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>

See Change C++/CLI project to another framework than 4.0 with vs2010

Community
  • 1
  • 1
Matt Smith
  • 17,026
  • 7
  • 53
  • 103