我如何决定对新的 C++ 项目使用 ATL、MFC、Win32 还是 CLR?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/821676/
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
How do I decide whether to use ATL, MFC, Win32 or CLR for a new C++ project?
提问by John M Gant
I'm just starting my first C++ project. I'm using Visual Studio 2008. It's a single-form Windows application that accesses a couple of databases and initiates a WebSphere MQ transaction. I basically understand the differences among ATL, MFC, Win32 (I'm a little hazy on that one actually) and CLR, but I'm at a loss as to how I should choose.
我刚刚开始我的第一个 C++ 项目。我正在使用Visual Studio 2008。它是一个单一形式的 Windows 应用程序,它访问几个数据库并启动一个 WebSphere MQ 事务。我基本上了解 ATL、MFC、Win32(实际上我对那个有点模糊)和 CLR 之间的区别,但我不知道应该如何选择。
Is one or more of these just there for backward-compatibility?
其中一个或多个是否只是为了向后兼容?
Is CLR a bad idea?
CLR是个坏主意吗?
Any suggestions appreciated.
任何建议表示赞赏。
Edit:I've chosen C++ for this project for reasons I didn't go into in the post, which are not entirely technical. So, assumingC++ is the only/best option, which should I choose?
编辑:由于我没有在帖子中讨论的原因,我为这个项目选择了 C++,这并不完全是技术性的。那么,假设C++ 是唯一/最好的选择,我应该选择哪个?
采纳答案by Reed Copsey
It depends on your needs.
这取决于您的需求。
Using the CLR will provide you with the most expressive set of libraries (the entire .NET framework), at the cost of restricting your executable to requiring the .NET framework to be installed at runtime, as well as limiting you to the Windows platform (however, all 4 listed technologies are windows only, so the platform limitation is probably the least troublesome).
使用 CLR 将为您提供最具表现力的一组库(整个 .NET 框架),代价是将您的可执行文件限制为要求在运行时安装 .NET 框架,并将您限制为 Windows 平台(但是,所有列出的 4 种技术都仅适用于 Windows,因此平台限制可能是最不麻烦的)。
However, CLR requires you to use the C++/CLI extensions to the C++ language, so you'll, in essense, need to learn some extra language features in order to use this. Doing so gives you many "extras," such as access to the .net libraries, full garbage collection, etc.
但是,CLR 要求您使用 C++/CLI 对 C++ 语言的扩展,因此从本质上讲,您需要学习一些额外的语言功能才能使用它。这样做会为您提供许多“附加功能”,例如访问 .net 库、完整的垃圾收集等。
ATL & MFC are somewhat trickier to decide between. I'd refer you to MSDN's page for choosingin order to decide between them. The nice thing about ATL/MFC is that you don't need the .NET framework, only the VC/MFC runtimes to be installed for deployment.
ATL 和 MFC 之间的决定有些棘手。我会向您推荐MSDN 的页面进行选择,以便在它们之间做出决定。ATL/MFC 的好处是您不需要 .NET 框架,只需要安装 VC/MFC 运行时进行部署。
Using Win32 directly provides the smallest executables, with the fewest dependencies, but is more work to write. You have the least amount of helper libraries, so you're writing more of the code.
直接使用 Win32 提供最小的可执行文件,依赖最少,但需要编写更多的工作。您拥有最少数量的帮助程序库,因此您要编写更多代码。
回答by arke
Win32 is the raw, bare-metal way of doing it. It's tedious, difficult to use, and has a lot of small details you need to remember otherwise things will fail in relatively mysterious ways.
Win32 是一种原始的裸机方式。它很乏味,难以使用,并且有很多你需要记住的小细节,否则事情会以相对神秘的方式失败。
MFC builds upon Win32 to provide you an object-oriented way of building your application. It's not a replacement for Win32, but rather an enhancement - it does a lot of the hard work for you.
MFC 建立在 Win32 之上,为您提供了一种构建应用程序的面向对象的方法。它不是 Win32 的替代品,而是一种增强功能 - 它为您做了很多艰苦的工作。
System.Windows.Forms (which is what I assume you meant by CLR) is completely different but has large similarities to MFC from its basic structure. It's by far the easiest to use but requires the .NET framework, which may or may not be a hindrance in your case.
System.Windows.Forms(这就是我假设您所说的 CLR)完全不同,但从其基本结构来看与 MFC 有很大的相似之处。它是迄今为止最容易使用的,但需要 .NET 框架,这在您的情况下可能会或可能不会成为障碍。
My recommendation: If you need to avoid .NET, then use MFC, otherwise use .NET (in fact, in that case, I'd use C# as it's much easier to work with).
我的建议:如果您需要避免使用 .NET,则使用 MFC,否则使用 .NET(实际上,在这种情况下,我会使用 C#,因为它更容易使用)。
回答by Rob
As far as C++ goes, I would use WTL. It's lightweght and you will have few (if any) dependencies, making it easy to ship and install. I find it very satisfying when my app consists of a single EXE that will run on most versions of Windows, but this may not be a concern to you.
就 C++ 而言,我会使用 WTL。它是轻量级的,您几乎没有(如果有)依赖项,因此易于运输和安装。当我的应用程序包含可在大多数 Windows 版本上运行的单个 EXE 时,我觉得非常令人满意,但这对您来说可能不是问题。
If you choose to go .NET instead, then C# is almost certainly the way to go.
如果您选择转而使用 .NET,那么 C# 几乎肯定是您要走的路。
More in WTL here:
更多关于 WTL 在这里:
回答by Clyde
I would be very curious as to why you would do this in C++ at all. Based on your brief description, C# sounds like a much more appropriate choice.
我很好奇你为什么要用 C++ 来做这件事。根据您的简要描述,C# 听起来是一个更合适的选择。
Just to elaborate a bit, look at the link you gave describing the C++ CLR. The top rated answer notes (accurately, in my opinion) that C++ is appropriate for "kernel, games, high-performance and server apps" - none of which seems to describe what you're doing.
只是详细说明一下,请查看您提供的描述 C++ CLR 的链接。评分最高的答案指出(准确地说,在我看来)C++ 适用于“内核、游戏、高性能和服务器应用程序”——这些似乎都没有描述你在做什么。
MFC, ATL, etc are going to be supported in the sense that, yes you'll be able to compile your app on future versions of Visual Studio and run them on future versions of Windows. But they're not supported in the sense that there's not a lot of new development going on in the API or the language the same way there is in the CLR and C#.
MFC、ATL 等将在某种意义上得到支持,是的,您将能够在未来版本的 Visual Studio 上编译您的应用程序并在未来版本的 Windows 上运行它们。但它们不受支持,因为 API 或语言中没有像 CLR 和 C# 中那样的大量新开发。
回答by Patrick
There is nothing wrong with CLR. Like others here I'd suggest C# but as you have reasons for sticking with C++ then using the .NET framework is several thousand times easier than messing with ATL/MFC if you're not already familiar with them (IMO).
CLR 没有任何问题。像这里的其他人一样,我建议使用 C#,但由于您有理由坚持使用 C++,如果您还不熟悉它们 (IMO),那么使用 .NET 框架比使用 ATL/MFC 容易数千倍。
It may be worth mentioning that if you're using C++/CLR then you're not really using C++ at all. C++/CLR compiles to CIL just like C#. I've never used it myself but I believe its purpose is to allow you to compile legacy code and make it easily available to new .NET code rather than allow new code work with old C++ executables. There are other methods of calling native code from .NET which, perhaps, you should explore.
值得一提的是,如果您使用的是 C++/CLR,那么您根本就没有真正使用 C++。C++/CLR 像 C# 一样编译为 CIL。我自己从未使用过它,但我相信它的目的是允许您编译遗留代码并使其易于用于新的 .NET 代码,而不是允许新代码与旧的 C++ 可执行文件一起使用。还有其他从 .NET 调用本机代码的方法,也许您应该探索一下。