简体   繁体   English

如何使C ++(共享)库与clang和GCC兼容?

[英]How do I make a C++ (shared) library compatible with clang and GCC?

I've written a fairly substantial C++11 library, and I'm planning to allow download of pre-compiled versions from my website. 我写了一个相当庞大的C ++ 11库,我打算允许从我的网站下载预编译版本。

So I've set up an automated build which compiles the library with clang and make it available for download, but this has exposed a problem: if I try to use the clang-compiled library with GCC I get undefined references (mainly related to std::string ). 所以我已经建立了一个自动构建,用clang编译库并使其可供下载,但这暴露了一个问题:如果我尝试使用GCC的clang编译库我得到未定义的引用(主要与std::string相关) std::string )。 I think this is related to the GCC dual-ABI changes in GCC 5.1 but I'm not sure how to fix it. 我认为这与GCC 5.1中的GCC双ABI变化有关,但我不确定如何解决它。

My question is, what flags should I set, or practices should I follow to make a C++ library compatible with both clang and GCC? 我的问题是,我应该设置什么标志,或者我应该遵循哪些做法使C ++库与clang和GCC兼容?

Or should I give up and compile two separate libraries? 或者我应该放弃并编译两个独立的库?

As already mentioned in several places (eg. here ) libc++ is not fully binary compatible with libstdc++. 正如在几个地方已经提到的那样(例如, 这里 ),libc ++与libstdc ++不是完全二进制兼容的。 There are several options, but some of them are somewhat not-so-straightforward. 有几个选项,但其中一些选项有点不那么直截了当。

  1. Compile two separate libraries - always working solution. 编译两个独立的库 - 始终工作解决方案。
  2. Remove incompatible containers from your interface (eg. std::string) - but this might be lot of work and sometimes not a good idea. 从您的界面中删除不兼容的容器(例如std :: string) - 但这可能是很多工作,有时候不是一个好主意。
  3. Instruct your library GCC users that you link with libc++ and that they need to do the same, basic steps here . 向您的库GCC用户指示您使用libc ++进行链接,并且他们需要在此处执行相同的基本步骤。 But I guess most GCC users do not want to do this. 但我想大多数GCC用户不想这样做。
  4. Use clang with libstdc++ using -stdlib=libstdc++ flag to be compatible with libstdc++ (as suggested in other answer). 使用带有libstdc ++的clang,使用-stdlib=libstdc++ flag与libstdc ++兼容(如其他答案所示)。 This solution might be harder to setup on some platforms though. 但是,在某些平台上设置此解决方案可能更难。

I would suggest as already mentioned in comments to go with option 1. 我建议如评论中已经提到的那样选择选项1。

There are several options: 有几种选择:

  1. Don't distribute it in binary form. 不要以二进制形式分发它。 Instead, make it easy to build everywhere (eg by using CMake, or autotools or ...) 相反,在任何地方都可以轻松构建(例如使用CMake或autotools或......)

  2. Make it header only. 仅限标题。 This is by far the simplest solution but might not be what you want. 这是迄今为止最简单的解决方案,但可能不是您想要的。 It only really makes sense for templated code, and incurs a heavy impact on compile-time performance of your library. 它只对模板化代码有意义,并且会对库的编译时性能产生严重影响。

  3. Tell people to link with libstdc++ when using Clang and your library. 使用Clang和您的库时,告诉人们链接libstdc ++。 Suboptimal solution (I for one like to check my code against libc++ as well as libstdc++), but (virtually) every Linux user has libstdc++ installed anyway. 次优解决方案(我喜欢检查我的代码与libc ++以及libstdc ++),但(实际上)每个Linux用户都安装了libstdc ++。 Make sure to pick a slightly older version (the one shipped in the latest Debian Stable distro is a good choice), because newer versions might introduce new symbols olders versions are missing. 确保选择稍微旧的版本(最新的Debian Stable发行版中提供的版本是一个不错的选择),因为较新的版本可能会引入新的符号缺少版本。 New versions should be ABI compatible anyway. 无论如何,新版本应该是ABI兼容的。

Note the situation for Visual Studio users is even worse, where every single compiler release mandates a new binary because they guarantee absolutely nothing with respect to the C++ libraries' or compiler's ABI. 请注意,Visual Studio用户的情况甚至更糟,每个编译器发布都需要一个新的二进制文件,因为它们对C ++库或编译器的ABI完全没有任何保证。

Another option is for your shared library to not expose any C++ standard library types in its interface. 另一个选择是您的共享库不在其接口中公开任何C ++标准库类型。 And have a header file supplied with your shared library that converts std::string to types consumed by your library, such as struct my_string_span { char const *begin, *end; }; 并且您的共享库提供了一个头文件,它将std::string转换为库所使用的类型,例如struct my_string_span { char const *begin, *end; }; struct my_string_span { char const *begin, *end; }; and other standard containers as necessary. 和其他标准容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM