gpt4 book ai didi

c++ - 链接器错误 - 带有 libboost_thread 的 macOS 上 undefined symbol std::string::c_str() const?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:07:13 25 4
gpt4 key购买 nike

我从 homebrew 安装了 boost 1.55.0在 macos 小牛队上。获取链接器异常 - 找不到 std::string::c_str() ,我不明白为什么。这可能是 Homebrew 软件的问题吗?我尝试直接从 boost 编译 boost 1.55.0,它甚至没有在 macOS 上构建。

这段代码:

#include <iostream>
#include <unordered_map>
#include <vector>
#include <boost/thread/tss.hpp>

typedef std::unordered_map<std::string, std::string> StringMap;
static boost::thread_specific_ptr<std::vector<StringMap*>> rlist;


int main()
{
return 0;
}

使用此命令行编译:

g++ -std=c++11 main.cpp -I/usr/local/include -L/usr/local/lib -lboost_thread -lboost_system -lboost_atomic -lboost_log -lstdc++

抛出这个链接器异常:

Undefined symbols for architecture x86_64:
"std::string::c_str() const", referenced from:
boost::system::system_error::what() const in libboost_thread.a(thread.o)
"std::string::empty() const", referenced from:
boost::system::system_error::what() const in libboost_thread.a(thread.o)
"std::allocator<char>::allocator()", referenced from:
boost::system::system_error::system_error(boost::system::error_code, char const*) in libboost_thread.a(thread.o)
boost::system::(anonymous namespace)::generic_error_category::message(int) const in libboost_system.a(error_code.o)
"std::allocator<char>::~allocator()", referenced from:
boost::system::system_error::system_error(boost::system::error_code, char const*) in libboost_thread.a(thread.o)
boost::system::(anonymous namespace)::generic_error_category::message(int) const in libboost_system.a(error_code.o)
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)", referenced from:
boost::system::system_error::system_error(boost::system::error_code, char const*) in libboost_thread.a(thread.o)
boost::system::(anonymous namespace)::generic_error_category::message(int) const in libboost_system.a(error_code.o)
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::string const&)", referenced from:
boost::thread_exception::thread_exception(boost::thread_exception const&) in libboost_thread.a(thread.o)
boost::condition_error::condition_error(boost::condition_error const&) in libboost_thread.a(thread.o)
boost::system::(anonymous namespace)::generic_error_category::message(int) const in libboost_system.a(error_code.o)
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string()", referenced from:
boost::system::system_error::system_error(boost::system::error_code, char const*) in libboost_thread.a(thread.o)
boost::system::(anonymous namespace)::generic_error_category::message(int) const in libboost_system.a(error_code.o)
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()", referenced from:
boost::system::system_error::what() const in libboost_thread.a(thread.o)
boost::system::system_error::~system_error() in libboost_thread.a(thread.o)
boost::system::system_error::~system_error() in libboost_thread.a(thread.o)
boost::thread_exception::~thread_exception() in libboost_thread.a(thread.o)
boost::thread_exception::~thread_exception() in libboost_thread.a(thread.o)
boost::thread_resource_error::~thread_resource_error() in libboost_thread.a(thread.o)
boost::thread_resource_error::~thread_resource_error() in libboost_thread.a(thread.o)
...
"std::string::operator=(char const*)", referenced from:
boost::system::system_error::what() const in libboost_thread.a(thread.o)
boost::system::(anonymous namespace)::generic_error_category::message(int) const in libboost_system.a(error_code.o)
"std::string::operator+=(char const*)", referenced from:
boost::system::system_error::what() const in libboost_thread.a(thread.o)
"std::string::operator+=(std::string const&)", referenced from:
boost::system::system_error::what() const in libboost_thread.a(thread.o)
"std::runtime_error::runtime_error(std::string const&)", referenced from:
boost::system::system_error::system_error(boost::system::error_code, char const*) in libboost_thread.a(thread.o)
"std::_Rb_tree_decrement(std::_Rb_tree_node_base*)", referenced from:
std::_Rb_tree<void const*, std::pair<void const* const, boost::detail::tss_data_node>, std::_Select1st<std::pair<void const* const, boost::detail::tss_data_node> >, std::less<void const*>, std::allocator<std::pair<void const* const, boost::detail::tss_data_node> > >::_M_insert_unique(std::pair<void const* const, boost::detail::tss_data_node> const&) in libboost_thread.a(thread.o)
"std::_Rb_tree_increment(std::_Rb_tree_node_base*)", referenced from:
_tls_destructor in libboost_thread.a(thread.o)
std::_Rb_tree<void const*, std::pair<void const* const, boost::detail::tss_data_node>, std::_Select1st<std::pair<void const* const, boost::detail::tss_data_node> >, std::less<void const*>, std::allocator<std::pair<void const* const, boost::detail::tss_data_node> > >::erase(std::_Rb_tree_iterator<std::pair<void const* const, boost::detail::tss_data_node> >, std::_Rb_tree_iterator<std::pair<void const* const, boost::detail::tss_data_node> >) in libboost_thread.a(thread.o)
"std::__throw_length_error(char const*)", referenced from:
std::vector<std::pair<boost::condition_variable*, boost::mutex*>, std::allocator<std::pair<boost::condition_variable*, boost::mutex*> > >::_M_insert_aux(__gnu_cxx::__normal_iterator<std::pair<boost::condition_variable*, boost::mutex*>*, std::vector<std::pair<boost::condition_variable*, boost::mutex*>, std::allocator<std::pair<boost::condition_variable*, boost::mutex*> > > >, std::pair<boost::condition_variable*, boost::mutex*> const&) in libboost_thread.a(thread.o)
"std::_Rb_tree_rebalance_for_erase(std::_Rb_tree_node_base*, std::_Rb_tree_node_base&)", referenced from:
_tls_destructor in libboost_thread.a(thread.o)
std::_Rb_tree<void const*, std::pair<void const* const, boost::detail::tss_data_node>, std::_Select1st<std::pair<void const* const, boost::detail::tss_data_node> >, std::less<void const*>, std::allocator<std::pair<void const* const, boost::detail::tss_data_node> > >::erase(std::_Rb_tree_iterator<std::pair<void const* const, boost::detail::tss_data_node> >, std::_Rb_tree_iterator<std::pair<void const* const, boost::detail::tss_data_node> >) in libboost_thread.a(thread.o)
"std::_Rb_tree_insert_and_rebalance(bool, std::_Rb_tree_node_base*, std::_Rb_tree_node_base*, std::_Rb_tree_node_base&)", referenced from:
std::_Rb_tree<void const*, std::pair<void const* const, boost::detail::tss_data_node>, std::_Select1st<std::pair<void const* const, boost::detail::tss_data_node> >, std::less<void const*>, std::allocator<std::pair<void const* const, boost::detail::tss_data_node> > >::_M_insert_unique(std::pair<void const* const, boost::detail::tss_data_node> const&) in libboost_thread.a(thread.o)
l

最佳答案

我的 spirit 力量说:

  • g++ 在你的系统上实际上是 clang,默认配置为使用 libc++。这是 Mac 上的常用配置。
  • Homebrew 的 boost 发行版是用 libstdc++ 编译的,也许还有真正的 g++。

这两个标准库不是二进制兼容的,因此会出现链接器错误。要解决此问题,请将 -stdlib=libstdc++ 传递给您的 clang-in-g++ 服装,或获取实际的 GCC(Homebrew 应该有一个发行版),或 build boost with libc++ .

复制和演示:

$ cat 1.cpp
#include <string>
#include <iostream>
int main() {
std::string s("Blah");
std::cout << s << std::endl;
}

$ g++ -v
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin13.2.0
Thread model: posix
$ g++ 1.cpp -c -stdlib=libstdc++ -o 1.o
$ g++ 1.o -lstdc++
Undefined symbols for architecture x86_64:
"std::allocator<char>::allocator()", referenced from:
_main in 1.o
"std::allocator<char>::~allocator()", referenced from:
_main in 1.o
"std::ostream::operator<<(std::ostream& (*)(std::ostream&))", referenced from:
_main in 1.o
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&)", referenced from:
_main in 1.o
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()", referenced from:
_main in 1.o
"std::ios_base::Init::Init()", referenced from:
___cxx_global_var_init in 1.o
"std::ios_base::Init::~Init()", referenced from:
___cxx_global_var_init in 1.o
"std::cout", referenced from:
_main in 1.o
"std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)", referenced from:
_main in 1.o
"std::basic_ostream<char, std::char_traits<char> >& std::operator<<<char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)", referenced from:
_main in 1.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

$ g++ -stdlib=libstdc++ 1.o && ./a.out
Blah

关于c++ - 链接器错误 - 带有 libboost_thread 的 macOS 上 undefined symbol std::string::c_str() const?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24718996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com