将 Delphi 代码转换为 C++ 的最佳方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/641140/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:26:24  来源:igfitidea点击:

Best way to convert Delphi code to C++?

c++delphimacos

提问by Dana Holt

I have an application written in Delphi that compiles in Delphi 2007. I think it was originally written in Delphi 7.

我有一个用 Delphi 编写的应用程序,它在 Delphi 2007 中编译。我认为它最初是用 Delphi 7 编写的。

Anyway, I need to convert all the core non-GUI code into C++ because I want to release a Mac version of the software.

无论如何,我需要将所有核心非 GUI 代码转换为 C++,因为我想发布该软件的 Mac 版本。

What is the best way to do this? Any shortcuts I can take to speed up the process?

做这个的最好方式是什么?我可以采取什么捷径来加快这个过程?

EDIT: The code compiles to native code, not .NET.

编辑:代码编译为本机代码,而不是 .NET。

回答by Mason Wheeler

Simple answer: You simply can't port non-trivial Delphi code to C++ without a complete rewrite. C++'s object model is very different from Delphi's. It doesn't have a base class like TObject from which all other objects are derived, and it lacks support for a lot of the RTTI stuff that Delphi code often takes for granted. And there's no simple way to reimplement Delphi RTTI in C++, since a lot of it's done at the compiler level, and a lot of it's based on the fact that all Delphi classes descend from TObject.

简单的回答:如果没有完全重写,您根本无法将重要的 Delphi 代码移植到 C++。C++ 的对象模型与 Delphi 的非常不同。它没有像 TObject 这样派生所有其他对象的基类,并且它缺乏对 Delphi 代码通常认为理所当然的许多 RTTI 内容的支持。并且没有简单的方法可以在 C++ 中重新实现 Delphi RTTI,因为很多都是在编译器级别完成的,而且很多都是基于所有 Delphi 类都源自 TObject 的事实。

C++ also lacks support for the concept of unit initializationand finalizationsections that are so common in Delphi, and what it has instead is badly broken. (Look up the "static order initialization fiasco" for all the gory details.)

C++ 也缺乏对Delphi 中很常见的单元初始化终结部分的概念的支持,而它所拥有的却被严重破坏了。(查看“静态订单初始化惨败”了解所有血腥细节。)

Delphi's exception handling is also much more advanced than C++'s. Part of this is the object model and part of it's compiler magic. Plus, C++ has no support for the try-finallyconstruct.

Delphi 的异常处理也比 C++ 先进得多。一部分是对象模型,一部分是编译器魔法。另外,C++ 不支持try-finally结构。

If you want to port a Delphi project to the Mac, Free Pascal is your best solution. It's not 100% compatible with Delphi, but it's good enough for a lot of things, and you specifically mentioned that you don't need to port the Delphi GUI stuff. AFAIK the GUI area is the source of most of FPC's compatibility weaknesses, so if that's not necessary, FPC is probably pretty close to ideal for your needs, at least until CodeGear gets an OSX compiler out. (Which hasn't been officially announced, but based on various things that have been said it's not unreasonable to suppose that one will be available sometime next year.)

如果您想将 Delphi 项目移植到 Mac,Free Pascal 是您最好的解决方案。它不是 100% 与 Delphi 兼容,但它对于很多事情来说已经足够了,而且你特别提到你不需要移植 Delphi GUI 的东西。AFAIK GUI 区域是大多数 FPC 兼容性弱点的根源,因此如果没有必要,FPC 可能非常接近您的需求,至少在 CodeGear 推出 OSX 编译器之前是这样。(尚未正式宣布,但基于各种说法,假设明年某个时候可以使用并不是没有道理的。)

回答by Dana Holt

For converting your code from Delphi to Cpp, have a look at

要将代码从 Delphi 转换为 Cpp,请查看

http://ivan.vecerina.com/code/delphi2cpp/.

http://ivan.vecerina.com/code/delphi2cpp/

I used this to convert some of the classes and functions in SysUtils, DateUtils and StrUtils using wxWidgets functions. If you are planning to use wxWidgets for C++ have a look at http://twinforms.com/products/wxwidgets/wxvcl.phpwhich has all the converted source.

我用它来转换使用 wxWidgets 函数的 SysUtils、DateUtils 和 StrUtils 中的一些类和函数。如果您打算将 wxWidgets 用于 C++,请查看http://twinforms.com/products/wxwidgets/wxvcl.php,其中包含所有转换后的源代码。

If you want directly develop Mac OSX applications using then have a look at wxForms for Delphi - http://twinforms.com/products/wxformsdelphi/index.php

如果您想使用直接开发 Mac OSX 应用程序,请查看用于 Delphi 的 wxForms - http://twinforms.com/products/wxformsdelphi/index.php

回答by Dana Holt

I think this would be difficult to do mechanically, so you are probably looking at a complete re-write. One thing to bear in mind is that typically Delphi uses try...finally structures for resource management, whereas C++ uses a technique known as RAII (resource acquisition is initialisation). You should read up on this and other C++ idioms before you attempt the conversion.

我认为这很难机械地完成,因此您可能正在考虑完全重写。要记住的一件事是,Delphi 通常使用 try...finally 结构进行资源管理,而 C++ 使用称为 RAII(资源获取即初始化)的技术。在尝试转换之前,您应该仔细阅读这个和其他 C++ 习语。

回答by Reed Copsey

If your code compiles in Delphi 2007 into .NET assemblies, you may have a much easier option than trying to port from Delphi's object pascal to C++.

如果您的代码在 Delphi 2007 中编译为 .NET 程序集,那么您可能有一个比尝试从 Delphi 的对象 pascal 移植到 C++ 更容易的选择。

You could potentially compile your logic into .NET assemblies (and maybe even portions of the UI), and use Mono to run it on Mac. You could write a custom GUI around Mono, or even potentially make a single, platform independent application.

您可以将您的逻辑编译成 .NET 程序集(甚至可能是 UI 的一部分),并使用 Mono 在 Mac 上运行它。您可以围绕 Mono 编写自定义 GUI,甚至可能制作一个独立于平台的应用程序。

回答by John Thomas

You can use also Delphi Prism. It's for .NET, but it's the last expression in Delphi language spec. It supports also Mac OSX (see the link). Also the guys from CodeGear/EMBT are in the works for a new compiler as well as for a new version of Delphi which is expected to enter in beta in April and narrow the gap between Prism and RAD studio. See their 'Beta Programs' page.

您也可以使用Delphi Prism。它用于 .NET,但它是 Delphi 语言规范中的最后一个表达式。它还支持 Mac OSX(请参阅链接)。来自 CodeGear/EMBT 的人也在为新的编译器以及新版本的 Delphi 工作,该版本预计将在 4 月进入测试版,并缩小 Prism 和 RAD 工作室之间的差距。请参阅他们的“测试版程序”页面。

回答by Tim Sullivan

The "correct" way to do this is to rewrite it in Objective C. I find Objective C a little weird, but there are a lot of similarities with Delphi in the way objects connect and delegate.

这样做的“正确”方法是用Objective C 重写它。我发现Objective C 有点奇怪,但是在对象连接和委托的方式上与Delphi 有很多相似之处。

You may be able to use Free Pascal to do it more quickly, but you should seriously consider a rewrite.

您也许可以使用 Free Pascal 更快地完成它,但您应该认真考虑重写。

I would be in love with Embarcadero if they could release a Mac OS X version of Delphi that didn't, you know, suck like Kylix did. One can dream.

如果 Embarcadero 能够发布一个 Mac OS X 版本的 Delphi,它不会像 Kylix 那样糟糕,我会爱上他们。一个人可以做梦。

Edit: There is a great benefit to staying in Delphi, and having a separate version for the Mac in Objective C. First, it means you don't need to rewrite the version on Windows, losing the (presumably) years of investment in Delphi code. Second, Mac software operates differently than Windows, from a UI perspective. A simple port of the product is inappropriate, and hobbles the developer from using the great native features of Windows and Mac. See: older versions of MS Word for Mac, or iTunes for Windows. They look and feel wrong.

编辑:留在 Delphi 中有很大的好处,并在 Objective C 中为 Mac 提供一个单独的版本。首先,这意味着您不需要在 Windows 上重写版本,从而失去了(大概)多年来对 Delphi 的投资代码。其次,从 UI 的角度来看,Mac 软件的运行方式与 Windows 不同。产品的简单移植是不合适的,并且阻碍了开发人员使用 Windows 和 Mac 的强大本机功能。请参阅:Mac 版 MS Word 的旧版本或 Windows 版 iTunes。他们看起来和感觉都错了。