针对不同操作系统编译C ++静态库的建议?

时间:2017-12-22 08:26:31

标签: c++ linux gcc boost

让我们说我使用CentOS 7和GCC 7来编译一个静态库(让我们称之为mylib.a)我想在CentOS 6上使用它(编译对象)系统库)。

最好的方法是什么?静态链接到libstdc ++等?还有什么?

目前,当我在我的静态库mylib.a上构建(在CentOS 6上使用gcc 4.x)可执行文件时,我得到类似的内容:

/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)'

我怀疑这是因为boost_date_time.a并不是针对libstdc ++构建的。当它是在没有最新开发工具的情况下在CentOS 7上使用gcc 4.x构建时。

思想?

谢谢!

2 个答案:

答案 0 :(得分:0)

构建在最老的受支持系统上 - 这种方式通常适用于Linux。较新版本的gcc和libstdc ++通常与旧版本兼容。

答案 1 :(得分:0)

您可以尝试使用静态链接 c 库 Musl,然后使用包括 libc 和 libstdc++ 在内的所有静态库构建您的程序,它是一个通用的 Linux 可执行程序。

Alpine disto 已经将 Musl 作为默认的 libc 库提供,所以我建议你使用 docker 镜像和 alpine 来构建带有 CI/CD 管道的程序。

但请注意 glibc 和 musl 之间存在一些细微差别,您可能需要更改程序以使其兼容 musl