Clang可以使用GCC编译的.a lib编译代码吗? [英] Can Clang compile code with GCC compiled .a libs?

查看:660
本文介绍了Clang可以使用GCC编译的.a lib编译代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在gcc下编译我的项目.它使用Boost,ZeroMQ作为静态.a库以及某些.so库(如SDL).我想一直走go,但现在不行.我想知道是否可以编译使用在ccc和clang下编译的.a.so库的代码?

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?

推荐答案

是的,您通常可以将clang与GCC编译的库一起使用(反之亦然,对gcc使用CLANG的编译库),因为实际上它不是编译而是相关的链接.您可能不走运,得到不愉快的惊喜.

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.

原则上,您可以依赖于用于链接相关库的libstdc++版本(如果它们是用C ++编码的).实际上,通常这并不重要.

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.

在C ++中,名称修改在理论上可能是一个问题(可能有一些极端的情况,甚至包括g++的两个不同版本之间的不兼容性.同样,在实践中,这通常不是问题.

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.

因此,通常您可以将CLANG(甚至是不同但接近的版本)与GCC混合使用,但是您可能会感到不愉快.任何C ++编译器(无论是CLANG还是GCC)都应该能够使用相同的编译器和版本(包括相同的 C ++标准库 实现).这就是为什么在发行版中升级编译器需要大量工作的原因:发行版制造商必须确保所有软件包都能很好地编译(而且确实会感到惊讶!).

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!).

请注意libstdc ++的版本确实很重要. C和GCC社区在努力使其ABI与编译器升级兼容,但是存在一些微不足道的情况.阅读特定和特定的C ++标准库实现的文档.这些特殊情况可以解释在GCC 8编译的代码中使用良好的C ++库二进制文件(与GCC 5编译)时发生的神秘崩溃.该错误不在库中,而是ABI不兼容地演变.

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.

这篇关于Clang可以使用GCC编译的.a lib编译代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆