Feb 25, 2011

Moving a C++ Project from VS2008 to VS2010

As I mentioned in my Learning F# post, I was prompted to upgrade from Visual Studio 2008 (Team Edition) to Visual Studio 2010 Professional. Usually upgrading an old solution poses no problems but my most recent product involved not just C# projects (which transitioned without problem), but also some C++/CLI projects, which were not so smooth. The worst part is it seemed to be made more difficult simply because the Visual C++  team couldn't be bothered to do it correctly.

The Easy Problem

I can't be sure this was strictly an upgrade issue as I had recently reorganized my folder structure for this project. I thought I had gone in and rebuilt the entire solution afterwards, but now I realize that since I had the C++ projects targeted to x86 (because of library limitations) and the C# targeted to any CPU, only the C# code was rebuilding. So perhaps I never did rebuild the C++ code, but either way several references were missing in the C++ projects. The solution was easy and I just added back the missing references.

The Harder Problem

Once the references were fixed, I got warnings about how the C++ code was targeting .NET 4.0 but the C# code was targeting .NET 3.5. This came as somewhat of a surprise as I obviously never told the C++ code to target .NET 4.0. I don't believe you can even do this under VS2008.

<RantWarning > Turns out the upgrade process did this to me silently, didn't even ask me politely to bend over. This strikes me as a bone-headed decision. I wouldn't even find it acceptable if they made the fact that they did this prominent in the log. Which by the way, they not only didn't make it prominent, as far as I can tell they don't even mention it. I checked the log and made sure I expanded the section for the .vcproj projects. At the very least a warning dialog should have been shown at the conclusion of the process, noting that this unnecessary and very unusual thing was done.

The best part is the twofold reason they give for doing this: both of which just reinforce the fact that C++ is considered a 2nd class language by Microsoft's Visual Studio team.

  1. They didn't want to bother making the compiler capable of targeting .NET 3.5. Can you imagine them making this decision about C# or even F# let alone VB? No way, you wouldn't upgrade if they did that. C++ programmers on the other hand get the, "frack 'em, C++ is hard they should use another language if they don't like it".
  2. They also didn't want to bother checking to see if you had VS2008 installed, and automatically targeting that compiler for you. I guess somehow they thought we would be UPGRADING a VS2008 solution but we would no longer have VS2008. Here's the "reasoning" behind this decision from the Visual C++ teams' own blog post on the subject:

"After conversion, managed C++ projects will target the 4.0 Framework by default.  The reason behind this design is that the VS2010 compiler cannot target Framework 2.0, 3.0 or 3.5."

imageThe emphasis is mine, because I find that such an acceptable explanation. Yep, it can't be done. The old compiler could do it, but we lost the recipe and now it CANNOT be done, no way, nuh uh. Does this remind anyone of when Scotty tells Kirk it will take 16 galactic years to get the warp engines on line; Kirk tells Scotty he has 15 minutes or they're all dead, so Scotty does it in 10? Scotty, it will cost us $50 million, your year-end bonus and I'll cut the salary of every team member by 25%. Captain, we had to stop work on all the easter eggs but it turns out it can be done! </RantWarning>

The hard part was finding their blog post. A few sentences after the lame rationalization, the post tells you how to modify the vcxproj file yourself, because they couldn't be bothered to do it.  You have to insert an entry in the first Property Group and name the new entry TargetFrameworkVersion and give it the value v3.5 as shown in the figure at left using and XML editor, or as shown below using a text editor.


About Me

My photo
Tod Gentille (@todgentille) is now a Curriculum Director for Pluralsight. He's been programming professionally since well before you were born and was a software consultant for most of his career. He's also a father, husband, drummer, and windsurfer. He wants to be a guitar player but he just hasn't got the chops for it.