Can compiling Clang code with GCC compiled .a libs?

I have my project, which is now compiled under gcc. It uses Boost, ZeroMQ as .a static libraries .a and some .so libraries such as SDL. I want to go, but not now. I wonder if it is possible to compile code that uses the .a and .so libraries that were compiled under gcc using clang?

+6
source share
2 answers

Yes, you can usually use clang with GCC compiled libraries (and vice versa, use gcc with CLANG compiled libraries) because this is actually not a compilation, but a link , which matters. You may be out of luck and get unpleasant surprises.

In principle, you may have some dependencies on the version of libstdc++ used to link the corresponding libraries (if they are encoded in C ++). Actually, this usually does not really matter.

In C ++, name mangling could theoretically be a problem (there may be some angular cases, even incompatibilities between two different versions of g++ ). Again, in practice this is usually not a problem.

As a rule, you can mix CLANG (even different but close versions) with GCC, but you may have unpleasant surprises. What you would expect from any C ++ compiler (be it CLANG or GCC) is simply the ability to compile and link all the software (and all libraries) together using the same compiler and version. This is why updating the compiler in the distribution is a lot of work: developers must make sure that all packages are compiled (and they really get surprises!).

+9
source

At least for the Crypto ++ library this does not work (verified :-(). So for C ++ code, it is unlikely to work, while pure c code will probably refer to OK.

EDIT: There was a problem with Mac OS X 10.9 Mavericks and Xcode-5, which switched the standard C ++ library for clang from libstdC ++ to libC ++. It did not exist on Mac OS X 10.8 or earlier.

The solution seems to be: if you need to compile C ++ code with clang and link it to a gcc-compiled library, use "clang ++ -stdlib = libstdC ++". Linking succeeds and the resulting binary executes correctly.

CAVEAT: it doesn't seem like this: even if you can compile a library compiled with "clang ++ -stdlib = libstdc ++" and link gcc-compiled code to it, this code will crash using SEGV. So far, I have found that the only way to link to a clang-compiled library is to compile your code with clang, not gcc.

+2
source

All Articles