Targeting C++/CLI Projects to .Net Framework 3.X in Visual Studio 2010

If you work with C++/CLI projects, and recently jumped to Visual Studio 2010, you have probably noticed that Visual Studio re-targeted your project to .Net Framework 4. This is the default behavior when converting C++/CLI projects to VS2010, as some of its C++ compiler tools are only compatible with .Net Framework 4.

The problem is that sometimes you cannot jump to .Net 4 yet. Maybe because customer’s restrictions, maybe because compatibility problems with 3rd party tools, whatever… If that’s your case, you need to target to .Net Framework 3.0 or 3.5, and still want to use VS2010, you can do the following:

(Source: here and here and even better here)

1.- Change the target Framework of your project

On a C# project, you can select the target Framework by simply changing a value in a combo box. On a C++/CLI project, you will find this in the project properties page:

It´s just a read-only value, that you cannot change directly. To change it, you must edit the project´s file (.vcxproj). To do so, open it in the notepad, or better, use the new feature included in VS2010: Edit project file by right-clicking in the project and selecting the following menu entry:

image

You will end up with an xml view of the file. Search for a node called PropertyGroup. As a child node of this one, you should create another one with the form:

<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>

It might already be there or not. If it’s not, create it. If it is, just edit its value, setting the target framework version you want: v3.0, v3.5, etc, ending with something like this:

image After the change, the project properties page will reflect the new target framework

2.- Reload the project

After any change in project’s file, you will need to reload it, by closing and re-opening the solution, or by selecting “Reload Project” in the context menu of VS.

3.- Install Visual Studio 2008 SP1

If you remember, we said that the VS2010 C++ compiler tools were not compatible with any Framework other than 4.0. That’s why as soon you change the target Framework and recompile, VS2010 will try to use the C++ compiler tools of Visual Studio 2008. If they are not present, it will stop the compilation with an error, requesting you to install VS2008.

Just a quick note: you need to install also the Service Pack 1 to VS2008.

4.- Issues

In my case, and with my specific project, the Microsoft C++/CLI Optimizing Compiler crashed if I targeted the project to .Net Framework 3.5. I really didn’t investigate why, as I really wanted to target v3.0 and didn’t have much time. Targeting v3.0 worked just fine.

So if you find the same error and find the reason or a solution, please comment!

Cheers!