Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using 32-bit library in 64-bit C++ program

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.

like image 474
zleba Avatar asked Jul 24 '11 23:07

zleba


2 Answers

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.

like image 55
Stephen Canon Avatar answered Oct 14 '22 21:10

Stephen Canon


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?

like image 26
Omnifarious Avatar answered Oct 14 '22 21:10

Omnifarious