gcc vs clang common library issue
Asked Answered
P

3

5

I have two applications, one compiled with gcc(c++) and another compiled with clang++. I am to use common shared boost library for both the applications. My question is whether to compile boost shared library using clang compiler or gcc compiler. Can I use boost library compiled with gcc in my application that is being compiled using clang?

Piperine answered 8/4, 2018 at 7:20 Comment(1)
Possible duplicate of Can Clang compile code with GCC compiled .a libs?Frausto
S
8

g++ and clang++ are compatible as compilers (because they both follow the Itanium ABI), but they may come with incompatible standard library implementations.

g++ comes with a standard library implementation called libstdc++. You can direct g++ to use a different implementation but this is not exactly trivial.

clang++ sometimes comes without a standard library implementation of its own (and is configured to use implementation provided by g++), and sometimes comes with an implementation called libc++. One can easily switch clang++ to use either libc++ or libstdc++ with a single command line option.

So your question boils down to what standard library implementation(s) your applications use. If they use the same implementation, you need to build Boost with that implementation (and either compiler). If they use different implementations, you need two separate builds of Boost.

Mixing components built against different standard library implementations in the same application can sometimes be done, but is not straightforward, entails a lot of restrictions, and with things like boost is either not feasible or downright impossible.

Seth answered 8/4, 2018 at 8:21 Comment(1)
very well explained. Thanks!Piperine
G
0

You need to use two separate builds of boost for each of your apps, unless the boost usage is limited to within a dynamic library that is then linked to by those apps and said dynamic library doesn't export types from the c++ std library within it

Giese answered 4/6, 2021 at 1:28 Comment(0)
C
-2

Short answer: maybe, maybe not.

Long answer:

The C++ standard (or any other standard) doesn't guarantee this sort of usage.

The developers of Clang do put reasonable effort into making Clang and GCC compatible, but for the most obvious (it is a different compiler!) and some less obvious reasons (subtle differences in understanding of the meaning of the C++ specification for example), they are not the same compiler and thus MAY do things differently in several aspects.

The most obvious reasons for incompatibility are:

  • differences in type definitions. Things like size_t, int and long may not be identically defined, so a function that is compiled with int func() may not return the same size value in both compilers.
  • differences in name mangling. How the compiler differentiates func(int) from func(long) for example.
  • differences in calling convention. How arguments are passed and returned - whether an argument is in a register or on the stack, which order the registers are picked for the first to last of register arguments, etc.
  • differences in exception handling. How the compiler generates the code to resolve an exception thrown in a particular place in the code, and unwinding back to the nearest catch statement.

That's by no means a complete list of

Some of the above differences may also depend on which target the code is compiled for - for example, it may work perfectly fine on x86, but compile for PowerPC, and there's some difference somewhere.

I don't believe anyone is able to conclusively say that "yes, this will work" or "no it won't". I do compile code with a mixture of clang and gcc quite regularly, and it does work almost all the time. But that doesn't mean it works ALL the time for ALL cases of everything. And that's the real issue.

If you have a good test-suite [if not, you probably ought to fix that anyways], you can verify that all of your (tested) code works as expected. That's a much better guarantee than "someone on some internet page wrote that it works", because it tests what you are using, in the way you are using it.

Crossbill answered 8/4, 2018 at 7:41 Comment(2)
Absolutely nothing like that can possibly happen. Millions of lines are compiled with clang++ and linked with g++-compiled code, including the g++-provided standard library libstdc++, with no single issue. Any such issue would be a clang++ bug. Seamless compatibility with g++ is one of clang++ strongest selling points.Seth
The things you list are actually the things that are most compatible between the two, because they are defined by the ABI (size of fundamental types, alignment, exception handling routines, calling convention, name mangling), which Clang obviously follows. The things that are different are e.g. builtin functions generated and the C++ library itself defining types like std::string differently. That last bit will be what prevents this from working of anything at all, not anything else.License

© 2022 - 2024 — McMap. All rights reserved.