简体   繁体   English

不同操作系统编译C++静态库的推荐?

[英]Recommendation for Compiling C++ Static Library for Different OSs?

Let's say I use CentOS 7 and GCC 7 to compile a static library (let's call it mylib.a) and I would like to use it (compiled against) on CentOS 6 (with older system libraries).假设我使用 CentOS 7 和 GCC 7 来编译一个静态库(我们称之为 mylib.a),并且我想在 CentOS 6(具有较旧的系统库)上使用它(编译)。

What's the best way to do so?这样做的最佳方法是什么? Static linking to libstdc++, etc?静态链接到 libstdc++ 等? What else?还有什么?

Currently, I am getting something like this when I built (with gcc 4.x on CentOS 6) an executable against my static library mylib.a:目前,当我构建(在 CentOS 6 上使用 gcc 4.x)针对我的静态库 mylib.a 的可执行文件时,我得到了这样的信息:

/opt/boost/boost/date_time/gregorian/gregorian_io.hpp:62: undefined reference to `operator delete(void*, unsigned long)'
mylib.a([myFile].o): In function `std::locale::locale<boost::date_time::date_facet<boost::gregorian::date, char, std::ostreambuf_iterator<char, std::char_traits<char> > > >(std::locale const&, boost::date_time::date_facet<boost::gregorian::date, char, std::ostreambuf_iterator<char, std::char_traits<char> > >*)':
/opt/rh/devtoolset-7/root/usr/include/c++/7/bits/locale_classes.tcc:47: undefined reference to `operator delete(void*, unsigned long)'

I suspect that it is because boost_date_time.a wasn't built against libstdc++.a when it was built with gcc 4.x on CentOS 7 without the latest dev tool.我怀疑这是因为 boost_date_time.a 在没有最新开发工具的情况下在 CentOS 7 上使用 gcc 4.x 构建时不是针对 libstdc++.a 构建的。

Thought?想法?

Thanks!谢谢!

Build on the oldest supported system - that way it usually works on Linux.建立在最古老的受支持系统上 - 这样它通常可以在 Linux 上运行。 Newer versions of gcc and libstdc++ is usually compatible with older ones.较新版本的 gcc 和 libstdc++ 通常与旧版本兼容。

You can have a try with Musl , the static linked c library, then build your program with all static libraries include libc and libstdc++, it's a universal Linux executable program.您可以尝试使用静态链接 c 库Musl ,然后使用包括 libc 和 libstdc++ 在内的所有静态库构建您的程序,它是一个通用的 Linux 可执行程序。

The Alpine disto has shipped Musl as the default libc library, so I suggest you use a docker image with alpine to build your program with CI/CD pipeline. Alpine disto 已将 Musl 作为默认 libc 库提供,因此我建议您使用 docker 镜像和 alpine 来构建带有 CI/CD 管道的程序。

But be aware there are some trivial differences between glibc and musl, you may need to change your program to make it musl compatible但是请注意 glibc 和 musl 之间存在一些细微差别,您可能需要更改程序以使其与 musl 兼容

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

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