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?
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.
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
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
andlong
may not be identically defined, so a function that is compiled withint func()
may not return the same size value in both compilers. - differences in name mangling. How the compiler differentiates
func(int)
fromfunc(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.
© 2022 - 2024 — McMap. All rights reserved.