在 64 位 C++ 中使用 32 位库程序
有什么方法可以在 64 位系统中使用旧的 32 位静态库 *.a 吗? 没有机会获得这个旧库的源代码来再次编译它。 我也不想在 gcc 中使用 -m32,因为该程序使用许多 64 位库。 谢谢。
Is there any way how use an old 32-bit static library *.a in a 64-bit system.
The is no chance to obtain a source code of this old library to compile it again.
I also do not want to use -m32 in gcc, because the program use many 64bit libraries.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这完全取决于您运行的平台。例如,PowerPC 上的 OS X 就可以“正常工作”。
在 x86 平台上,您无法将 32 位库链接到 64 位可执行文件。如果您确实需要使用该库,则需要启动一个单独的 32 位进程来处理对该库的调用,并使用某种形式的 IPC 在 64 位应用程序和该帮助程序进程之间传递这些调用。预先警告:这会带来很多麻烦。在开始这条路之前,请确保您确实需要该库。
That depends entirely on the platform on which you're running. OS X on PowerPC, for example, that would "Just Work".
On x86 platforms, you can't link a 32-bit library into a 64-bit executable. If you really need to use that library, you'll need to start a separate 32-bit process to handle your calls to the library, and use some form of IPC to pass those calls between your 64-bit application and that helper process. Be forewarned: this is a lot of hassle. Make sure that you really need that library before starting down this road.
在x86/x86_64平台上,你不能这样做。我的意思是,如果您为每个您想要调用的 32 位函数编写自定义汇编语言包装器,也许您可以做到。但这是唯一可能的方法。即使你愿意做这项工作,我也不确定它是否有效。
原因是调用约定完全不同。 x864_64 平台有更多寄存器可供使用,所有操作系统的 64 位 ABI(应用程序二进制接口,基本上如何传递参数、如何设置堆栈帧等)标准都使用这些寄存器用于参数传递等的额外寄存器。
这使得32位和64位x86/x86_64系统的ABI完全不兼容。你必须编写一个翻译层。而且 32 位 ABI 可能允许 32 位代码来摆弄 64 位代码不允许摆弄的 CPU 内容,这会让你的工作变得更加困难,因为你需要恢复可能被修改的内容返回 64 位代码之前的状态。
这甚至还没有讨论指针这个问题。如何将位于 64 位地址的数据结构的指针传递给 32 位代码?
On the x86/x86_64 platform, you can't do this. I mean, maybe you could if you wrote custom assembly language wrappers for each and every 32 bit function you wanted to call. But that's the only way it's even possible. And even if you were willing to do that work I'm not sure it would work.
The reason for this is that the calling conventions are completely different. The x864_64 platform has many more registers to play with, and the 64-bit ABI (Application Binary Interface, basically how parameters are passed, how a stack frame is set up and things like that) standards for all of the OSes make use of these extra registers for parameter passing and the like.
This makes the ABI of 32-bit and 64-bit x86/x86_64 systems completely incompatible. You'd have to write a translation layer. And it's possible the 32-bit ABI allows 32-bit code to fiddle around with CPU stuff that 64-bit code is not allowed to fiddle with, and that would make your job even harder since you'd be required to restore the possibly modified state before returning to the 64-bit code.
And that's not even talking about this issue of pointers. How do you pass a pointer to a data structure that's sitting at a 64-bit address to 32-bit code?
您需要使用
-m32
来加载 32 位库。也许您最好的方法是创建一个包装该库的服务器。然后,64 位应用程序可以使用 IPC(各种方法,例如套接字、fifo)来与托管该库的进程进行通信。
在 Windows 上,这称为进程外 COM。我不知道unix上有类似的框架,但是相同的方法可以工作。
You need to use
-m32
in order to load a 32-bit library.Probably your best approach is to create a server wrapping the library. Then a 64-bit application can use IPC (various methods, e.g. sockets, fifos) in order to communicate to and from the process hosting the library.
On Windows this would be called out-of-process COM. I don't know that there's a similar framework on unix, but the same approach will work.
简单的答案:你不能。
Simple answer: You can't .