C++ 与 C++.NET 之间的主要区别是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8710581/
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
What is the main difference between C++ vs C++.NET?
提问by olidev
Possible Duplicate:
What is the difference between Managed C++ and C++/CLI?
What is CLI/C++ exactly? How does it differ to 'normal' c++?
可能的重复:
托管 C++ 和 C++/CLI 之间有什么区别?
CLI/C++ 究竟是什么?它与“普通”C++ 有何不同?
I am in doubt of distinguishing between C++ and C++.NET.
我怀疑区分 C++ 和 C++.NET。
Is that right C++ is unmanaged code and C++.NET is managed code?
C++ 是非托管代码而 C++.NET 是托管代码吗?
I need to program for a project in C++. For better building the GUI, I would prefer to use C++.NET.
我需要用 C++ 为一个项目编程。为了更好地构建 GUI,我更喜欢使用 C++.NET。
I also have another plain C++ library (unmanaged C++ DLL file), will it be possible to use it as a normal DLL library in the C++.NET project?
我还有另一个普通的 C++ 库(非托管 C++ DLL 文件),是否可以将它用作 C++.NET 项目中的普通 DLL 库?
回答by In silico
Is that right C++ is unmanaged code and C++.NET is managed code.
C++ 是非托管代码,C++.NET 是托管代码,对吗?
There's no such thing as "C++.NET". There's C++/CLI, which is basically C++ with Microsoft extensions that allow you to write code targeting the .NET framework. C++/CLI code compiles to CLR bytecode, and runs on a virtual machine just like C#. I'll assume you're actually talking about C++/CLI.
没有“C++.NET”这样的东西。有 C++/CLI,它基本上是带有 Microsoft 扩展的 C++,允许您编写面向 .NET 框架的代码。C++/CLI 代码编译为 CLR 字节码,并像 C# 一样在虚拟机上运行。我假设您实际上是在谈论 C++/CLI。
With respect to that, one can say standard C++ is unmanaged and C++/CLI is managed, but that's very much Microsoft terminology. You'll never see the term "unmanaged" used this way when talking about standard C++ unless in comparison with C++/CLI.
就这一点而言,可以说标准 C++ 是非托管的,而 C++/CLI 是托管的,但这是 Microsoft 的术语。除非与 C++/CLI 进行比较,否则在谈论标准 C++ 时,您永远不会看到以这种方式使用的术语“非托管”。
Both standard C++ and C++/CLI can be compiled by the same Visual C++ compiler. The former is the default on VC++ compilers, while a compiler switch is needed to make it compile in latter mode.
标准 C++ 和 C++/CLI 都可以由同一个 Visual C++ 编译器编译。前者是 VC++ 编译器的默认设置,而需要编译器开关才能使其在后一种模式下编译。
I need to program for a project in C++. For better building the GUI, I would prefer to use C++.NET.
我需要用 C++ 为一个项目编程。为了更好地构建 GUI,我更喜欢使用 C++.NET。
You can build GUI programs in C++ just as well as C++/CLI. It's just harder because there isn't a standard library in standard C++ for building GUI like the .NET framework has, but there are lots of projects out there like Qtand wxWidgetswhich provide a C++ GUI framework.
您可以使用 C++ 和 C++/CLI 构建 GUI 程序。只是更难,因为标准 C++ 中没有一个标准库来像 .NET 框架那样构建 GUI,但是有很多项目,比如Qt和wxWidgets,它们提供了一个 C++ GUI 框架。
I also have another plain C++ library (unmanaged C++ dll), will it be possible to use it as a normal dll library in the C++.NET project?
我还有另一个普通的 C++ 库(非托管 C++ dll),是否可以将它用作 C++.NET 项目中的普通 dll 库?
Yes. It might take some extra work to deal with the different standard C++ data types and .NET data types, but you can certainly make it work.
是的。处理不同的标准 C++ 数据类型和 .NET 数据类型可能需要一些额外的工作,但您当然可以使其工作。
回答by Software_Designer
Managed C++ is a now deprecatedMicrosoft set of deviations from C++, including grammatical and syntactic extensions, keywords and attributes, to bring the C++ syntax and language to the .NET Framework. These extensions allowed C++ code to be targeted to the Common Language Runtime (CLR) in the form of managed code as well as continue to interoperate with native code. Managed C++ was not a complete standalone, or full-fledged programming language.
Managed C++ 是Microsoft现已弃用的一组与 C++ 的偏差,包括语法和句法扩展、关键字和属性,用于将 C++ 语法和语言引入 .NET Framework。这些扩展允许 C++ 代码以托管代码的形式面向公共语言运行时 (CLR),并继续与本机代码进行互操作。托管 C++ 不是一个完整的独立或成熟的编程语言。
Managed C++
托管 C++
#using <mscorlib.dll>
using namespace System;
int main() {
Console::WriteLine("Hello, world!");
return 0;
}
Vanilla C++
香草 C++
#include <iostream>
using namespace std;
int main()
{
cout << "Hello, world!";
return 0;
}
回答by C Johnson
Well... C++ .NET is kind of a misnomer. You can program in C++ using visual studio .NET. Well that's what it was called along time ago. Now a days folks just call it Visual Studio, with the dot NET moniker. Well, at least the splash screen doesn't have a big ol .NET in the logo anymore.
嗯... C++ .NET 有点用词不当。您可以使用 Visual Studio .NET 在 C++ 中进行编程。嗯,很久以前就是这么叫的。现在人们只是称它为 Visual Studio,带有 .NET 绰号。好吧,至少启动画面的徽标中不再有大的 ol .NET。
It is kind of understood that using Visual Studio (VS), you can program in managed and unmanaged languages (Lots of choices there btw).
可以理解的是,使用 Visual Studio (VS),您可以使用托管和非托管语言进行编程(顺便说一句,有很多选择)。
If you want to program in C++ using Visual Studio you have two choices:
如果您想使用 Visual Studio 在 C++ 中编程,您有两种选择:
Unmanaged or native C/C++. This is the old (or new I guess too) C++ that you have always known, and you program with unmanaged memory.
Managed C++. They call this C++/CLI. That is read C++ over CLI, not C++ divided by CLI! This is C++ that has extra keywords, and a few extra syntax elements than the native C++. This allows you to utilize the .NET Foundation Class Library and do other fun things in the .NET framework. This of course uses the garbage collector for memory for managed types.
非托管或本机 C/C++。这是您一直都知道的旧的(或我猜也是新的)C++,并且您使用非托管内存进行编程。
托管 C++。他们称之为 C++/CLI。那是通过 CLI 读取 C++,而不是通过 CLI 划分的 C++!这是比原生 C++ 具有额外关键字和一些额外语法元素的 C++。这允许您利用 .NET 基础类库并在 .NET 框架中做其他有趣的事情。这当然使用垃圾收集器来为托管类型存储内存。
Personally my favorite language is C#, but if you need to interop between C++ and .NET than definitely use Managed C++. It is very easy to do, and I think is easier than that other P/Invoke stuff.
就个人而言,我最喜欢的语言是 C#,但如果您需要在 C++ 和 .NET 之间进行互操作,那么肯定使用托管 C++。这很容易做到,我认为比其他 P/Invoke 的东西更容易。
If you are going to some project, I would suggest you do your UI in C# and take advantage of all that it has to offer. Then have that reference a mixed mode managed library that contains your C++ code. I think that will be a lot easier for you.
如果你要去某个项目,我建议你用 C# 做你的 UI 并利用它所提供的一切。然后让该引用包含您的 C++ 代码的混合模式托管库。我认为这对你来说会容易得多。
The answer to your last question is yes, you can definitely use that in your app.
你最后一个问题的答案是肯定的,你绝对可以在你的应用程序中使用它。
Here is how the dependencies would work:
以下是依赖项的工作方式:
[C# App/GUI] depends on [Managed C++ assembly] depends on [Native C++ Lib]
[C# App/GUI] 依赖于 [Managed C++ assembly] 依赖于 [Native C++ Lib]
回答by juergen d
- Yes, C++ is unmanaged code and C++/CLI is managed.
- Yes, you can use your unmanaged C++ DLL in your C++/CLI project. But you have to write a wrapper for that. That means you have to define the unmanaged methods you want to access in your C++/CLI project.
- 是的,C++ 是非托管代码,而 C++/CLI 是托管代码。
- 是的,您可以在 C++/CLI 项目中使用非托管 C++ DLL。但是你必须为此编写一个包装器。这意味着您必须在 C++/CLI 项目中定义要访问的非托管方法。
Example:
例子:
using System.Runtime.InteropServices;
[DllImport("YourDLLName")]
public static extern void UnmanagedMethodName(string parameter1);