0

I am learning more about shared libraries and also linking in general, and I seem to have confused myself.

I understand (correct me if i am wrong) that a compiled library or object file in c++ may not be compatible with another library even on the same target architecture, if the compilers are different and that this is due to name mangling and the Application Binary interface being different in OS/Compiler/Architecture etc.

This I believe is also true for C libraries although this may have more chance to work (I am not totally sure that's right)

And I believe the above applies to both static and shared/dynamic libraries

But then I get very confused, because say i make a new Visual Studio VC++ project, and it will link with say kernel32.dll or Gdi32.lib, well I have no idea what these were compiled with but my guess is it wasn't VS2017's Visual C++ Compiler so how can this be linked yet I can't link to say something compiled with MinGW or maybe even something with an earlier VS release? Surely the Windows dlls have been compiled differently?

I guess this also applies to linux libraries I am assuming the shared object files could have been compiled with a different version of gcc/g++, and I imaging yum installing a devel package would give libs that might not work?

And finally if someone has a closed source library and sell that library if the compiler and version matters does that mean that in order to sell a DLL or SO you need to compile it for more or less every compiler and version?

I have read a lot of questions and answers on here and tried to do a lot of searching but I am stuck and could really use some help in understanding this.

B2020
  • 43
  • 6
  • [Application binary interface](https://en.wikipedia.org/wiki/Application_binary_interface) – Barmar Oct 04 '20 at 10:47
  • Does this answer your question? [How does calling a function from a shared library work?](https://stackoverflow.com/questions/58572341/how-does-calling-a-function-from-a-shared-library-work) – Akib Azmain Turja Oct 04 '20 at 10:55
  • @Barmar I have read the wiki, but I still am not understanding. I think my question was too long and didn't explain correctly. Is the ABI not defined by the compiler? (sorry when hit enter it submitted) If it is defined by the compiler then wouldn't VS's C++ compiler be different to what compiled Gdi32.dll? How are they compatible with one another? – B2020 Oct 05 '20 at 01:05
  • I have done more research and found this: https://stackoverflow.com/questions/41975672/abi-compatibility-of-visual-studio-c-libraries So if Windows dlls expose a C interface is this also true of linux? If that is true then is it best practice or standard to expose a extern C interface or is it that only a c api offers compatibility and the reason why Gdi32/kerel32 and linux libs are compatible is that they are C libs and not c++ libs. – B2020 Oct 05 '20 at 05:42
  • https://softwareengineering.stackexchange.com/questions/235706/are-c-object-files-created-with-different-compilers-binary-compatible Is another link I found that maybe covers the question, but i am left confused why do some libraries have an ABI and others don't, I assume windows and linux shared libraries have ABIs but when we compile with GCC/MSVC we don't have an ABI or it uses the compilers ABI When we make a dynamic library or shared library why is the platform's ABI not used, wouldn't this make the library work across all versions of Visual Studio? – B2020 Oct 05 '20 at 11:07
  • As explained in your linked answers, it's not the libraries that have an ABI, but the OS defines an ABI for shared C libraries. So you can't use shared C++ libraries of different compilers together, but it's possible for shared C libraries. – ssbssa Oct 05 '20 at 14:09
  • @ssbssa And that's because the C++ compiler has its own ABI but for C it is the OS that has the ABI? If that's the case would exposing a extern c interface fix the issue? – B2020 Oct 05 '20 at 22:27
  • For the most part, yes, this will fix the issue. You can't use exceptions/classes/references/... across the library boundaries. – ssbssa Oct 06 '20 at 00:15

0 Answers0