28

clang++ and g++ are ABI incompatible, even for things as core as standard containers, according to, e.g., the clang++ website.

Debian ships with C++ shared libraries, i.e. libboost, etc... that are compiled with ~something and user programs using both compiler generally work, and the library names aren't mangled with the compiler that was used for them. When you install clang, debian doesn't go and pull in duplicate versions of every C++ library installed on your system.

What's the deal? Is the ability of clang to link against distro-provided C++ libraries just way stronger than the (thankfully cautious) compiler devs describe it to be?

psmears
  • 26,070
  • 4
  • 40
  • 48
Andrew Wagner
  • 22,677
  • 21
  • 86
  • 100
  • 3
    I thought that actually, clang is supposed to be ABI compatible with gcc, otherwise you are right that this would be a huge nightmare. i'm not certain though. I thought clang basically needed to adopt this policy to get off the ground. – Chris Beck Sep 07 '15 at 17:43
  • 1
    Somewhat related: https://stackoverflow.com/questions/11682748/is-clang-abi-same-as-g?rq=1 – edmz Sep 07 '15 at 17:49
  • I wouldn't want to be saying they're a duplicate, exactly. That question refers to very specific versions. – Puppy Sep 07 '15 at 18:32
  • 2
    libc++ is not compatible with libstdc++ but clang with libstdc++ is supposed to be compatible with g++ with libstdc++. – ysdx Sep 07 '15 at 22:01

3 Answers3

31

even for things as core as standard containers

Standard containers are not all that "core". (For typical implementations) they are implemented entirely in valid C++ in headers, and if you compile the same headers with G++ and Clang++ you'll get ABI compatible output. You should only get incompatibilities "even for things as core as standard containers" if you use different versions of the container headers, not just by using Clang instead of GCC.

Both GCC and Clang conform to a cross-vendor, cross-platform C++ ABI (originally developed for the Itanium architecture, but also used for x86, x86_64, SPARC etc.) The really core things such as class layout, name mangling, exception handling, vtables etc. are specified by that ABI and Clang and GCC both follow it.

So in other words, if you compile the same source with GCC and Clang you'll get ABI-compatible binaries.

If you want to understand this stuff better see my What's an ABI and why is it so complicated? slides.

Nan Xiao
  • 16,671
  • 18
  • 103
  • 164
Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
24

G++ and Clang are for the vast majority completely ABI compatible. Furthermore, ABI incompatibilities for Standard containers are properties of the standard library implementation (libstdc++ or libc++), not the compiler. Therefore, there is no need for any re-compilation.

Clang could never have gotten off the ground if it was not ABI compatible with g++, as it would be basically unusable without a pre-existing large following. In fact, Clang is so compatible with GCC, they ape virtually all of g++'s command-line interface, compiler intrinsics, bugs, etc, so that you can literally just drop in Clang instead of G++ and the vast majority of the time, everything will just work.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • This is precisely correct. Clang uses GCC's name-mangling and GCC's calling conventions. It's so compatible with GCC that, on OS X with Xcode, `g++` is effectively a symlink to `clang++`. – nneonneo Sep 07 '15 at 18:02
  • 3
    To be more correct, it's the ABI's, which is not GCCs. Itanium ABI is independent of GCC. – Puppy Sep 07 '15 at 18:24
  • 1
    I had never heard of that. Can you give some examples of g++ bugs that clang++ mimics? – Theodoros Chatzigiannakis Sep 07 '15 at 19:32
2

This probably will not answer the exact question correctly:

Some time ago I tried to compile some object files wih gcc, another object files with clang. Finally I linked everything together and it worked correctly.

I believe Linux distributions uses gcc, because I examined some Makefile's of Ubuntu and CentOS and they used gcc.

Nick
  • 9,962
  • 4
  • 42
  • 80
  • 1
    Not all distributions, e.g. OpenMandriva uses Clang for nearly the whole dsitro. – Jonathan Wakely Sep 07 '15 at 18:12
  • I am on Centos and I tried linking an static library built using clang with an executable being built using g++ and I am getting linker errors which clearly indicate ABI incompatibility – Yug Singh Jul 15 '20 at 17:56