简体   繁体   English

德尔福dcu to obj

[英]Delphi dcu to obj

Is there a way to convert a Delphi .dcu file to an .obj file so that it can be linked using a compiler like GCC? 有没有办法将Delphi .dcu文件转换为.obj文件,以便可以使用像GCC这样的编译器进行链接? I've not used Delphi for a couple of years but would like to use if for a project again if this is possible. 我没有使用Delphi几年,但是如果有可能的话,我想再次使用if。

Delphi can output .obj files, but they are in a 32-bit variant of Intel OMF. Delphi可以输出.obj文件,但它们是Intel OMF的32位变体。 GCC, on the other hand, works with ELF (Linux, most Unixes), COFF (on Windows) or Mach-O (Mac). 另一方面,GCC与ELF(Linux,大多数Unix),COFF(在Windows上)或Mach-O(Mac)一起使用。

But that alone is not enough. 但仅凭这一点还不够。 It's hard to write much code without using the runtime library, and the implementation of the runtime library will be dependent on low-level details of the compiler and linker architecture, for things like correct order of initialization. 在不使用运行时库的情况下编写很多代码很困难,并且运行时库的实现将依赖于编译器和链接器体系结构的低级细节,例如正确的初始化顺序。

Moreover, there's more to compatibility than just the object file format; 而且,兼容性不仅仅是目标文件格式; code on Linux, in particular, needs to be position-independent, which means it can't use absolute values to reference global symbols, but rather must index all its global data from a register or relative to the instruction pointer, so that the code can be relocated in memory without rewriting references. 特别是Linux上的代码需要与位置无关,这意味着它不能使用绝对值来引用全局符号,而是必须从寄存器或相对于指令指针索引其所有全局数据,以便代码可以在内存中重新定位而无需重写引用。

DCU files are a serialization of the Delphi symbol tables and code generated for each proc, and are thus highly dependent on the implementation details of the compiler, which changes from one version to the next. DCU文件是Delphi符号表的序列化和为每个proc生成的代码,因此高度依赖于编译器的实现细节,编译器从一个版本更改为下一个版本。

All this is to say that it's unlikely that you'd be able to get much Delphi (dcc32) code linking into a GNU environment, unless you restricted yourself to the absolute minimum of non-managed data types (no strings, no interfaces) and procedural code (no classes, no initialization section, no data that needs initialization, etc.) 所有这一切都是说你不可能获得很多链接到GNU环境的Delphi(dcc32)代码,除非你把自己限制在绝对最小的非托管数据类型(没有字符串,没有接口)和程序代码(没有类,没有初始化部分,没有需要初始化的数据等)

Yes. 是。 Have a look at the Project Options dialog box: 查看“项目选项”对话框:


(High-Res) (高分辨率)

(answer to various FPC remarks, but I need more room) (回答各种FPC评论,但我需要更多空间)

For a good understanding, you have to know that a delphi .dcu translates to two differernt FPC files, .ppu file with the mentioned symtable stuff, which includes non linkable code like inline functions and generic definitions and a .o which is mingw compatible (COFF) on Windows. 为了更好地理解,您必须知道delphi .dcu转换为两个不同的FPC文件,带有上述symtable内容的.ppu文件,其中包括非链接代码(如内联函数和泛型定义)和.o(mingw兼容)( COFF)在Windows上。 Cygwin is mingw compatible too on linking level (but runtime is different and scary). Cygwin在链接级别上也兼容(但运行时不同且可怕)。 Anyway, mingw32/64 is our reference gcc on Windows. 无论如何,mingw32 / 64是我们在Windows上的参考gcc。

The PPU has a similar version problem as Delphi's DCU, probably for the same reasons. PPU与Delphi的DCU有类似的版本问题,原因可能相同。 The ppu format is different nearly every major release. ppu格式几乎与每个主要版本不同。 (so 2.0, 2.2, 2.4), and changes typically 2-3 times an year in the trunk (所以2.0,2.2,2.4),并且在一年中通常每年更换2-3次

So while FPC on Windows uses own assemblers and linkers, the .o's it generates are still compatible with mingw32 In general FPC's output is very gcc compatible, and it is often possible to link in gcc static libs directly, allowing eg mysql and postgres linklibs to be linked into apps with a suitable license. 因此,虽然Windows上的FPC使用自己的汇编程序和链接器,但它生成的.o仍然与mingw32兼容。通常FPC的输出非常兼容gcc,并且通常可以直接链接到gcc静态库中,允许例如mysql和postgres linklibs链接到具有合适许可证的应用程序。 (like eg GPL) On 64-bit they should be compatible too, but this is probably less tested than win32. (例如GPL)在64位上它们也应兼容,但这可能不如win32测试。

The textmode IDE even links in the entire GDB debugger in library form. textmode IDE甚至以库的形式链接整个GDB调试器。 GDB is one of the main reasons for gcc compatibility on Windows. GDB是Windows上gcc兼容性的主要原因之一。

While Barry's points about the runtime in general hold for FPC too, it might be slightly easier to work around this. 虽然Barry关于运行时的观点一般也适用于FPC,但解决这个问题可能会稍微容易一些。 It might only require calling certain functions to initialize the FPC rtl from your startup code, and similarly for the finalize. 它可能只需要调用某些函数来从您的启动代码初始化FPC rtl,并且类似于最终化。 Compile a minimal FPC program with -al and see the resulting assembler (in the .s file, most notably initializeunits and finalizeunits) Moreover the RTL is more flexible and probably more easily cut down to a minimum. 使用-al编译最小FPC程序并查看生成的汇编程序(在.s文件中,最值得注意的是initializeunits和finalizeunits)此外,RTL更灵活,可能更容易减少到最小。

Of course as soon as you also require exceptions to work across gcc<->fpc bounderies you are out of luck. 当然,只要你还需要例外工作来跨越gcc < - > fpc bounderies,你就不走运了。 FPC does not use SEH, or any scheme compatible with anything else ATM. FPC不使用SEH,也不使用任何与ATM相关的方案。 (contrary to Delphi, which uses SEH, which at least in theory should give you an advantage there, Barry?) OTOH, gcc might use its own libunwind instead of SEH. (与使用SEH的Delphi相反,至少在理论上应该给你一个优势,Barry?)OTOH,gcc可能会使用自己的libunwind而不是SEH。

Note that the default calling convention of FPC on x86 is Delphi compatible register, so you might need to insert proper cdecl (which should be gcc compatible) modifiers, or even can set it for entire units at a time using {$calling cdecl} 请注意,x86上FPC的默认调用约定是Delphi兼容寄存器,因此您可能需要插入适当的cdecl(应该是gcc兼容)修饰符,或者甚至可以使用{$ calling cdecl}一次为整个单元设置它

On *nix this is bog standard (eg apache modules), I don't know many people that do this on win32 though. 在* nix这是沼泽标准(例如apache模块),我不知道很多人在win32上这样做。

About compatibility; 关于兼容性; FPC can compile packages like Indy, Teechart, Zeos, ICS, Synapse, VST and reams more with little or no mods. FPC可以编译像Indy,Teechart,Zeos,ICS,Synapse,VST这样的软件包,并且只需要很少或没有mod就可以进行更多编辑。 The dialect levels of released versions are a mix of D7 and up, with the focus on D7. 释放版本的方言级别是D7及以上的混合,重点是D7。 The dialect level is slowly creeping to D2006 level in trunk versions. 在行李箱版本中,方言等级正逐渐爬升至D2006级别。 (with for in, class abstract etc) (用于in,class abstract等)

As far as I am aware, Delphi only supports the OMF object file format. 据我所知,Delphi只支持OMF目标文件格式。 You may want to try an object format converter such as Agner Fog's . 您可能想尝试像Agner Fog这样的对象格式转换器。

Since the DCU format is proprietary and has a tendency of changing from one version of Delphi to the next, there's probably no reliable way to convert a DCU to an OBJ. 由于DCU格式是专有的并且倾向于从一个版本的Delphi更改为下一个版本,因此可能没有可靠的方法将DCU转换为OBJ。 Your best bet is to build them in OBJ format in the first place, as per Andreas's answer. 根据Andreas的回答,你最好的选择是首先用OBJ格式构建它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM