当前位置: 首页 > 知识库问答 >
问题:

Ubuntu 20.04上Boost线程的Clang 10链接错误

郑正文
2023-03-14

从带有clang/lld 7和boost 1.65的Ubuntu 18.04升级到带有clang/lld 10和boost 1.71的20.04后,我遇到了一个不知道如何解决的链接器错误。Ubuntu20.04上的clang和boost是不兼容的对吗?在Ubuntu20.04上使用clang进行编译时,我有什么选择?

这个错误的最小例子是

#include <boost/thread/shared_mutex.hpp>
int main() {
  boost::shared_mutex sm;
}

备注:

  • 它与GCC 9和gold Linker配合使用很好。
  • clang+gold链接器组合失败,错误消息非常相同,bfd链接器失败,错误消息非常短,我将在下面显示输出。
  • 同样的错误发生在CLANG/LLD 9而不是10。

我创建了一个很容易在Ubuntu20.04系统或容器中复制的最小示例,我使用了DockerHub的简单ubuntu:focal容器进行复制。

  1. 安装CLANG/LLD 10和boost 1.71
apt update && apt install clang lld libboost-thread-dev
update-alternatives --install "/usr/bin/ld" "ld" "/usr/bin/ld.lld-10" 30

现在安装了所有需求,LLD是默认链接器。

root@d66452260792:/# cat x.cpp 
#include <boost/thread/shared_mutex.hpp>
int main() {
  boost::shared_mutex sm;
}

root@d66452260792:/# clang -lboost_thread x.cpp
successful compilation
  • 带有BFD链接器(GNU ld 2.34):
/usr/bin/ld: /tmp/x-a145e4.o: undefined reference to symbol '_ZTVN10__cxxabiv121__vmi_class_type_infoE@@CXXABI_1.3'
/usr/bin/ld: /lib/x86_64-linux-gnu/libstdc++.so.6: error adding symbols: DSO missing from command line
clang: error: linker command failed with exit code 1 (use -v to see invocation)
  • 使用LLD 10作为链接器(但黄金链接器错误看起来非常相似):
ld: error: undefined symbol: std::allocator<char>::allocator()
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::system::detail::generic_error_category_message[abi:cxx11](int))

ld: error: undefined symbol: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::system::detail::generic_error_category_message[abi:cxx11](int))

ld: error: undefined symbol: std::allocator<char>::~allocator()
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::system::detail::generic_error_category_message[abi:cxx11](int))
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::system::detail::generic_error_category_message[abi:cxx11](int))

ld: error: undefined symbol: std::_V2::generic_category()
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::system::detail::std_category::equivalent(int, std::error_condition const&) const)
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::system::detail::std_category::equivalent(std::error_code const&, int) const)
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::system::detail::std_category::equivalent(std::error_code const&, int) const)

ld: error: undefined symbol: typeinfo for std::_V2::error_category
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::system::detail::std_category::equivalent(int, std::error_condition const&) const)
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::system::detail::std_category::equivalent(std::error_code const&, int) const)
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(typeinfo for boost::system::detail::std_category)

ld: error: undefined symbol: __dynamic_cast
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::system::detail::std_category::equivalent(int, std::error_condition const&) const)
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::system::detail::std_category::equivalent(std::error_code const&, int) const)

ld: error: undefined symbol: __cxa_begin_catch
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(__clang_call_terminate)
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::system::system_error::what() const)
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::system::error_category::message(int, char*, unsigned long) const)
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(void std::_Rb_tree<boost::system::error_category const*, std::pair<boost::system::error_category const* const, std::unique_ptr<boost::system::detail::std_category, std::default_delete<boost::system::detail::std_category> > >, std::_Select1st<std::pair<boost::system::error_category const* const, std::unique_ptr<boost::system::detail::std_category, std::default_delete<boost::system::detail::std_category> > > >, boost::system::detail::cat_ptr_less, std::allocator<std::pair<boost::system::error_category const* const, std::unique_ptr<boost::system::detail::std_category, std::default_delete<boost::system::detail::std_category> > > > >::_M_construct_node<std::pair<boost::system::error_category const* const, std::unique_ptr<boost::system::detail::std_category, std::default_delete<boost::system::detail::std_category> > > >(std::_Rb_tree_node<std::pair<boost::system::error_category const* const, std::unique_ptr<boost::system::detail::std_category, std::default_delete<boost::system::detail::std_category> > > >*, std::pair<boost::system::error_category const* const, std::unique_ptr<boost::system::detail::std_category, std::default_delete<boost::system::detail::std_category> > >&&))

ld: error: undefined symbol: std::terminate()
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(__clang_call_terminate)

ld: error: undefined symbol: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::empty() const
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::system::system_error::what() const)
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::system::system_error::what() const)

ld: error: undefined symbol: std::runtime_error::what() const
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::system::system_error::what() const)
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::system::system_error::what() const)

ld: error: undefined symbol: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator=(char const*)
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::system::system_error::what() const)

ld: error: undefined symbol: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator+=(char const*)
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::system::system_error::what() const)

ld: error: undefined symbol: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator+=(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::system::system_error::what() const)

ld: error: undefined symbol: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::system::system_error::what() const)
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::system::system_error::what() const)
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::system::error_category::message(int, char*, unsigned long) const)
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::system::system_error::~system_error())

ld: error: undefined symbol: __cxa_end_catch
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::system::system_error::what() const)
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::system::error_category::message(int, char*, unsigned long) const)
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(void std::_Rb_tree<boost::system::error_category const*, std::pair<boost::system::error_category const* const, std::unique_ptr<boost::system::detail::std_category, std::default_delete<boost::system::detail::std_category> > >, std::_Select1st<std::pair<boost::system::error_category const* const, std::unique_ptr<boost::system::detail::std_category, std::default_delete<boost::system::detail::std_category> > > >, boost::system::detail::cat_ptr_less, std::allocator<std::pair<boost::system::error_category const* const, std::unique_ptr<boost::system::detail::std_category, std::default_delete<boost::system::detail::std_category> > > > >::_M_construct_node<std::pair<boost::system::error_category const* const, std::unique_ptr<boost::system::detail::std_category, std::default_delete<boost::system::detail::std_category> > > >(std::_Rb_tree_node<std::pair<boost::system::error_category const* const, std::unique_ptr<boost::system::detail::std_category, std::default_delete<boost::system::detail::std_category> > > >*, std::pair<boost::system::error_category const* const, std::unique_ptr<boost::system::detail::std_category, std::default_delete<boost::system::detail::std_category> > >&&))

ld: error: undefined symbol: std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::c_str() const
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::system::system_error::what() const)
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::system::error_category::message(int, char*, unsigned long) const)

ld: error: undefined symbol: std::_V2::error_category::~error_category()
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::system::detail::std_category::~std_category())

ld: error: undefined symbol: operator delete(void*)
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::system::detail::std_category::~std_category())
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::system::system_error::~system_error())
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::system::detail::to_std_category(boost::system::error_category const&))
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(__gnu_cxx::new_allocator<std::_Rb_tree_node<std::pair<boost::system::error_category const* const, std::unique_ptr<boost::system::detail::std_category, std::default_delete<boost::system::detail::std_category> > > > >::deallocate(std::_Rb_tree_node<std::pair<boost::system::error_category const* const, std::unique_ptr<boost::system::detail::std_category, std::default_delete<boost::system::detail::std_category> > > >*, unsigned long))
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::exception_detail::error_info_injector<boost::thread_resource_error>::~error_info_injector())
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::thread_resource_error::~thread_resource_error())
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::thread_exception::~thread_exception())
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::thread_resource_error> >::~clone_impl())
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::thread_resource_error> >::clone() const)
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::wrapexcept<boost::thread_resource_error>::~wrapexcept())

ld: error: undefined symbol: std::runtime_error::~runtime_error()
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::system::system_error::~system_error())
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::system::system_error::system_error(boost::system::system_error const&))

ld: error: undefined symbol: __cxa_guard_acquire
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::system::detail::to_std_category(boost::system::error_category const&))
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::system::detail::to_std_category(boost::system::error_category const&))
>>> referenced by x.cpp
>>>               /tmp/x-dd4c59.o:(boost::system::detail::to_std_category(boost::system::error_category const&))

ld: error: too many errors emitted, stopping now (use -error-limit=0 to see all errors)
clang: error: linker command failed with exit code 1 (use -v to see invocation)

共有1个答案

周通
2023-03-14

首先,与图书馆链接时,秩序很重要。始终将库放在命令行的最后一个。

其次,boost_thread依赖于boost_system库(应该列在boost_thread之前)。

再次,clang前端程序是用来链接C程序的,而不是C++。对于C++,请使用clang++

所以综合起来说:

$ clang++ x.cpp -lboost_system -lboost_thread
 类似资料:
  • 问题内容: 我正在尝试构建Boost并生成dll,但是我所做的任何事情都无法解决问题。 这是我得到的: 创建库bin.v2 \ libs \ thread \ build \ msvc-11.0 \ dbg \ adrs-mdl-32 \ async- excpt-on \ thrd-mlt \ boost_thread-vc110-mt-gd-1_53.lib和对象bin.v2 \ libs \

  • 问题内容: 我正在尝试使用Boost的Asio构建项目,但遇到了一些麻烦。最初,我试图在没有任何其他库的情况下构建项目,因为所有内容都应该放在头文件中。 我尝试构建的程序如下所示: 可以发现这里在加速的网站。 因此,最初我只有: 这导致以下错误: 看来我需要系统库。因此,我按照此处的入门指南中的说明进行操作,该指南为我提供了位于 / usr / include / boost_1_40_0 / s

  • 我正在尝试学习一些RxJava和RxAndroid,我认为我遇到的问题可以很容易地使用这样的工具来解决。问题是:我们可以在一个活动中有“N”个视图,并且每个视图都用于满足某些条件。当用户按“保存”时,我们想检查所有视图中是否满足所有条件,如果不满足,请用户分别确认每个视图。所以这是我在没有RxJava的情况下如何处理这个问题的示例: 显然,我需要某种类型的监听器来确认结果,并且在条件被确认后(使用

  • 我正在开发利用RxJava、realm和改进的应用程序。 我需要创建非常具体的数据处理链。我需要在io调度程序上执行改装调用,然后在我的自定义单线程领域调度程序上处理提供的数据,然后将结果推送到主线程调度程序上的ui。我试图通过使用多个组合来实现这一点,包括观察(observeOn)和订阅(subscribeOn),但我无法让中间部分在调度程序(scheduler)上执行。 我的目标是这样的

  • 我有安装,以便在所有以前版本的Android中验证所需的应用程序链接。然而,这种验证在Android 12(目前为Beta 2)中不再有效<代码>adb shell pm获取应用程序链接 Android12中的应用链接验证似乎有一些变化,但是留档并没有非常明确地说明需要更改什么以及更改是否向下兼容。