Linker error - Undefined symbols std::string::c_st

2019-04-29 04:28发布

问题:

I installed boost 1.55.0 from homebrew on macos mavericks. Getting a linker exception - not finding std::string::c_str() which I don't understand why. Could this be a problem with homebrew? I tried compiling boost 1.55.0 directly from boost and it doesn't even build on macos.

This little bit of code:

#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;
}

compiled with this command line:

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

throws this linker exception:

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

回答1:

My psychic powers say:

  • g++ on your system is actually clang, configured to use libc++ by default. This is the usual configuration on Macs.
  • The homebrew boost distribution was compiled with libstdc++, and maybe the real g++.

The two standard libraries are not binary compatible, hence the linker error. To fix this, pass -stdlib=libstdc++ to your clang-in-g++'s clothing, or get the actual GCC (Homebrew should have a distribution), or build boost with libc++.

Repro & demo:

$ 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