Duc*_*een 13 linux gcc clang static-libraries static-linking
I have my project currently compiling under gcc. It uses Boost, ZeroMQ as static .a
libraries and some .so
libraries like SDL. I want to go clang all the way but not right now. I wonder if it is possible to compile code that uses .a
and .so
libraries that were compiled under gcc with clang?
Bas*_*tch 17
Yes, you usually can use clang
with GCC compiled libraries (and vice versa, use gcc
with CLANG compiled libraries), because in fact it is not compilation but linking which is relevant. You might be unlucky and get unpleasant suprises.
You could in principle have some dependencies on the version of libstdc++
used to link the relevant libraries (if they are coded in C++). Actually, that usually does not matter much.
In C++, name mangling might in theory be an issue (there might be some corner cases, even incompatibilities between two different versions of g++
). Again, in practice it is usually not an issue.
So usually you can mix CLANG (even different but close versions of it) with GCC but you may have unpleasant surprises. What should be expected from any C++ compiler (be it CLANG or GCC) is just to be able to compile and link an entire software (and all libraries) together using the same compiler and version (and that includes the same C++ standard library implementation). This is why upgrading a compiler in a distribution is a lot of work: the distribution makers have to ensure that all the packages compile well (and they do get surprises!).
Beware that the version of libstdc++ does matter. Both Clang & GCC communities work hard to make its ABI compatible for compiler upgrades, but there are subtle corner cases. Read the documentation of your particular and specific C++ standard library implementation. These corner cases could explain mysterious crashes when using a good C++ library binary (compiled with GCC 5) in your code compiled with GCC 8. The bug is not in the library, but the ABI evolved incompatibly.
至少对于Crypto++ 库来说,这是行不通的(已验证:-( )。因此,对于 C++ 代码,它不太可能工作,而纯 C 代码可能会链接正常。
编辑:问题开始出现在 Mac OS X 10.9 Mavericks 和 Xcode-5 上,它将 clang 的默认 C++ 库从 libstdc++ 切换为 libc++。它在 Mac OS X 10.8 及更早版本中不存在。
解决方案似乎是:如果您需要使用 clang 编译 C++ 代码,并将其链接到 gcc 编译的库,请使用“clang++ -stdlib=libstdc++”。链接成功,生成的二进制文件正确运行。
注意:它似乎不能以其他方式工作:即使您可以构建使用“clang++ -stdlib=libstdc++”编译的库并将 gcc 编译的代码与其链接,该代码也会因 SEGV 而崩溃。到目前为止,我发现与 clang 编译的库链接的唯一方法是使用 clang 而不是 gcc 编译代码。
EDIT2:GCC-12 似乎包含-stdlib=
标志。g++ -stdlib=libc++
编译会创建Clang++
兼容的目标文件。很不错。