.net 使用 vs2010 将 C++/CLI 项目更改为 4.0 以外的其他框架
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2921702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change C++/CLI project to another framework than 4.0 with vs2010
提问by codymanix
Since I upgraded my project to visual studio 2010 project format, my C++/CLI project is targeted to .net framework 4.0.
由于我将项目升级为 Visual Studio 2010 项目格式,因此我的 C++/CLI 项目面向 .net framework 4.0。
It is easy to switch the framework version to another version from a C# project, but I have no clue how to do this in a C++/CLI project, I see no setting for this in the project property pages.
将框架版本从 C# 项目切换到另一个版本很容易,但我不知道如何在 C++/CLI 项目中执行此操作,我在项目属性页中没有看到此设置。
回答by Hans Passant
This shows up when you press F1 in the Framework and References dialog:
当您在“框架和引用”对话框中按 F1 时,会显示:
By default for new projects, the targeted framework is set to .NET Framework 4. The IDE does not support modifying the targeted framework, but you can change it manually. In the project file (.vcxproj), the default targeted framework is represented by the v4.0 property element. To change the targeted framework, unload the project, use a text editor to open the project file, and then change the value of the property element from v4.0 to another version that is installed on your server. For example, if you specify v3.5, which represents the .NET Framework v3.5, Visual Studio 2008 SP1 must be installed. Save and close the file, reload the project, and verify that the targeted framework is displayed in the property page.*
默认情况下,对于新项目,目标框架设置为 .NET Framework 4。IDE 不支持修改目标框架,但您可以手动更改它。在项目文件 (.vcxproj) 中,默认目标框架由 v4.0 属性元素表示。要更改目标框架,请卸载项目,使用文本编辑器打开项目文件,然后将属性元素的值从 v4.0 更改为安装在服务器上的另一个版本。例如,如果指定代表 .NET Framework v3.5 的 v3.5,则必须安装 Visual Studio 2008 SP1。保存并关闭文件,重新加载项目,并验证目标框架是否显示在属性页中。*
That's not terribly accurate on converted projects, you'll have to add the <TargetFrameworkVersion>element yourself. Put it in the PropertyGroup labeled "Globals":
这在转换后的项目上不是很准确,您必须<TargetFrameworkVersion>自己添加元素。将其放入标有“Globals”的 PropertyGroup 中:
<PropertyGroup Label="Globals">
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<others...>
</PropertyGroup>
The story is different when you use VS2012 and up, the first version of VS that acquired the Platform Toolset setting in the General property page. You must then select "v90" to get a proper build that targets 3.5. It is however clumsy, you must have all intermediate versions of VS installed on the machine to have that selection available.
当您使用 VS2012 及更高版本时,情况就不同了,VS 的第一个版本在 General 属性页中获得了 Platform Toolset 设置。然后,您必须选择“v90”以获得针对 3.5 的正确构建。然而它很笨拙,您必须在机器上安装所有中间版本的 VS 才能使用该选项。
Why you need VS2008 installed requires an explanation by itself. The core issue is that the C runtime library (msvcrt100.dll and up) contains .NET code to support managed code execution. The crucial detail is a module initializer that ensures the CRT is correctly initialized in program that uses C++/CLI code. That code always targets .NET 4 and since it is hard-baked into msvcrt100.dll (and up) you always have a rock-hard dependency on the v4.0.30319 runtime. You can only ever have a pure v2.0.50727 dependency when you use the old C runtime, msvcrt90.dll. You can only be sure that you have a msvcrt90.dll dependency when you use the compiler's #include files of VS2008.
为什么需要安装 VS2008 需要自己解释。核心问题是 C 运行时库(msvcrt100.dll 及更高版本)包含支持托管代码执行的 .NET 代码。关键的细节是一个模块初始化器,它确保在使用 C++/CLI 代码的程序中正确初始化 CRT。该代码始终以 .NET 4 为目标,并且由于它被硬烘焙到 msvcrt100.dll(及更高版本)中,因此您始终对 v4.0.30319 运行时具有坚如磐石的依赖性。当您使用旧的 C 运行时 msvcrt90.dll 时,您只能拥有纯 v2.0.50727 依赖项。只有在使用VS2008编译器的#include文件时,才能确定是否有msvcrt90.dll依赖。
Cold hard fact that it is pretty necessary to move to .NET 4 soon, you'll struggle with build problems like this if you don't. There are very few practical obstacles to that, .NET 4 is widely available for free on all targets you'd imagine. Overcoming the FUD that is associated with moving to a higher runtime version is generally only the real issue. No reasons for fear and doubt, it is stable.
冷酷的事实是,很快就迁移到 .NET 4 是非常必要的,如果你不这样做,你会遇到这样的构建问题。对此几乎没有实际障碍,.NET 4 可以在您想象的所有目标上广泛免费使用。克服与迁移到更高运行时版本相关的 FUD 通常只是真正的问题。没有恐惧和怀疑的理由,它是稳定的。
回答by Brian R. Bondy
Yes it is possible to change the target even for managed C++ projects:
是的,即使对于托管 C++ 项目,也可以更改目标:
Changing the Target .NET Framework for C++/CLI (VS 2010) To change the version of the .NET Framework for C++/CLI projects (VS 2010)
Right click on project in Solution Explorer and click Unload project Right click on unloaded project in Solution Explorer and select Edit
<projectname>.vcxprojIn project XML file locate node<PropertyGroup Label="Globals">In that node locate node<TargetFrameworkVersion>(if the node cannot be found, add it) Inner text of the node defines target framework. It can be v2.0,v3.0, v3.5 or v4.0 Save vcxproj file and close it Right click on unloaded project in Solution Explorer and click Reload Project Example<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>Note: These steps apply only for Visual Studio 2010 as it uses new format of C++ project files.
更改 C++/CLI 目标 .NET Framework (VS 2010) 更改 C++/CLI 项目的 .NET Framework 版本 (VS 2010)
右键单击解决方案资源管理器中的项目,然后单击卸载项目 右键单击解决方案资源管理器中卸载的项目,然后选择
<projectname>.vcxproj在项目 XML 文件中编辑 定位节点<PropertyGroup Label="Globals">在该节点中定位节点<TargetFrameworkVersion>(如果找不到该节点,则添加它) 节点的内部文本定义目标框架。它可以是 v2.0、v3.0、v3.5 或 v4.0 保存 vcxproj 文件并关闭它在解决方案资源管理器中右键单击卸载的项目,然后单击重新加载项目示例<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>注意:这些步骤仅适用于 Visual Studio 2010,因为它使用新格式的 C++ 项目文件。
回答by hakre
by an anonymous user:
匿名用户:
(Editing as I am a new user and cannot respond to this, whomever sees this feel free to submit the following) Changing the Toolset to v100 actually causes VS2010 to target .NET 4.0, even though it will still show up as targetting 3.5 in the project properties. VS2010 should really spit out a warning about this, because currently it appears as you though you can target .NET 3.5 with the v100 toolset, which you can't.
(编辑,因为我是一个新用户,无法对此做出回应,看到这一点的人可以随意提交以下内容)将工具集更改为 v100 实际上会导致 VS2010 以 .NET 4.0 为目标,即使它仍然会显示为目标 3.5 在项目属性。VS2010 真的应该对此发出警告,因为目前它看起来像您,虽然您可以使用 v100 工具集定位 .NET 3.5,但您不能。
回答by nche
In VS 2010 if the toolset is installed go to project properties->config properties->general and change Platform Toolset from v90 to v100.
在 VS 2010 中,如果安装了工具集,请转到项目属性-> 配置属性-> 常规并将平台工具集从 v90 更改为 v100。

