24

Possible Duplicate:
GCC 4.0, 4.2 and LLVM ABI Compatibility

As per subject, are both C++ ABIs compatible?
I.e. can a binary (Shared Object) generated with the former be used and linked with the latter (and vice-versa)?

Cheers

Community
  • 1
  • 1
Emanuele
  • 1,408
  • 1
  • 15
  • 39
  • The very general answer here is going to be no. But I think it's going to depend on exactly what you're compiling and for what architecture. – Falmarri Jul 27 '12 at 06:54
  • I guess that my question is not _just_ related to the libstdc++ compatibility, but even _your own_ _C++_ shared objects. – Emanuele Jul 27 '12 at 09:47
  • Now anymore: https://llvm.org/bugs/show_bug.cgi?id=23529 . gcc 5.1 introduced a new abi, which is not yet implemented by clang – ibre5041 Jul 31 '16 at 20:53

2 Answers2

18

According to the clang libc++ page, they're targeting

ABI compatibility with gcc's libstdc++ for some low-level features such as exception objects, rtti and memory allocation.

which seems to imply that they're not targeting 100% compatibility. For example, on that page they also say:

From years of experience (including having implemented the standard library before), we've learned many things about implementing the standard containers which require ABI breakage and fundamental changes to how they are implemented. For example, it is generally accepted that building std::string using the "short string optimization" instead of using Copy On Write (COW) is a superior approach for multicore machines (particularly in C++'0x, which has rvalue references). Breaking ABI compatibility with old versions of the library was determined to be critical to achieving the performance goals of libc++.

I believe that GCC is still using a reference-counted COW, so it appears that clang isn't worried about ABI compatibility with std::string (either with older clang compiled binaries or with GCC).

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • 4
    The same with VC++. "In every major version (VS 2005, 2008, 2010, 2012, etc.) we change the representations of STL objects like string and vector, making them binary-incompatible. We now have linker checks that prevent mixing object files/static libraries compiled with different major versions (2010+)..." (see http://social.msdn.microsoft.com/forums/en-us/vcgeneral/thread/8F2843AE-010D-4440-AD1B-9F021BB74C2E) – SChepurin Jul 27 '12 at 08:22
  • I guess instead the _C_ ABI is 100% compatible, right? – Emanuele Jul 27 '12 at 09:44
  • 14
    `libc++` is not the same as Clang, I think. Why does the anwser focus on standard library compatibility, not language binary compatibility? Is it obvious that they use the same calling conventions for methods, that they return objects on stack and generally are binary compatible on language level? – cubuspl42 Jun 22 '14 at 00:48
  • 5
    As the above comment correctly points out, ABI compatibility of the standard library is not the same as the ABI of the compiler. For example `clang++ -stdlib=libstdc++` will generate code that is 100% ABI-compatible with g++. But nowadays, clang uses `-stdlib=libc++` by default (instead of `stdlibc++`). So -- although it was wrong at the time it was posted -- this answer has accidentally become correct in the intervening years, at least on OSX. (When this answer was written, clang's default setting was `-stdlib=libstdc++`, so binaries were compatible with g++ by default.) – Stuart Berg Sep 24 '15 at 17:45
  • BTW, since gcc 5.1 libstdc++ no longer defaults to COW strings, because C++11 forbids them, changing the ABI. (Some distros, e.g. Slackware, continue to use the old ABI). libstdc++ [continues to support both ABIs](https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html) but you can't mix object files/libraries (aside from libstdc++) with the different ABIs together. – Ralph Versteegen Mar 06 '17 at 23:56
3

It seems to be compatible. Clang also has a project for their own C++ runtime, and it states that it is low-level compatible with GNU stdlibc++. I just tried a small example program, where I compiled one file with clang++, and compiled and linked the main program and with g++. No problem so far, but the program was rather simple.

Arne
  • 2,624
  • 3
  • 24
  • 45