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

Can I use the old 32-bit * .a static library on a 64-bit system. It is not possible to get the source code of this old library to compile it again. I also do not want to use -m32 in gcc, because the program uses many 64-bit libraries. Thanks.

+6
source share
4 answers

It completely depends on the platform on which you work. OS X on PowerPC, for example, "Just Work".

On x86 platforms, you cannot associate a 32-bit library with a 64-bit executable. If you really need to use this library, you will need to start a separate 32-bit process for processing calls in the library and use some form of IPC to transfer these calls between your 64-bit application and this helper process. Be warned: this is a lot of trouble. Before you start this road, make sure that you really need.

+6
source

On the x86 / x86_64 platform, you cannot do this. I mean, maybe you could if you wrote custom assembly shells for every 32-bit function that you wanted to call. But this is the only way that this is possible. And even if you were ready to do this work, Iโ€™m not sure that it will work.

The reason for this is that the calling conventions are completely different. The x864_64 platform has much more registers for playback, and 64-bit ABI (Application Binary Interface, basically, how parameters are passed, how the stack frame is set, and the like), standards for all operating systems use these additional registers to pass parameters, etc. .

This makes the ABI of 32-bit and 64-bit x86 / x86_64 systems completely incompatible. You will need to write a translation layer. And perhaps the 32-bit version of ABI allows 32-bit code to play with the processor, so that 64-bit code is not allowed to play, and this will make your work even harder, since you will need to restore what was possibly changed before returning to 64-bit code.

And that doesnโ€™t even talk about this pointer problem. How to pass a pointer to a data structure that is on a 64-bit address up to 32-bit code?

+1
source

The simple answer is: you cannot.

0
source

To download the 32-bit library you need to use -m32 .

Perhaps your best approach is to create a server that wraps the library. Then the 64-bit application can use IPC (various methods, for example, sockets, fifos) to interact with the hosting of the library and from it.

On Windows, this will be called out-of-process COM. I do not know that Unix has a similar structure, but the same approach will work.

0
source

Source: https://habr.com/ru/post/893465/


All Articles