我在macos mavericks上从
homebrew安装了1.55.0的提升.获取链接器异常 – 找不到std :: string :: c_str(),我不明白为什么.这可能是自制软件的问题吗?我尝试直接从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()",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&)",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::allocator<char> >::basic_string()",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&)",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*,boost::detail::tss_data_node> > >::erase(std::_Rb_tree_iterator<std::pair<void const* const,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::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&)",boost::detail::tss_data_node> >) in libboost_thread.a(thread.o) "std::_Rb_tree_insert_and_rebalance(bool,std::_Rb_tree_node_base*,boost::detail::tss_data_node> const&) in libboost_thread.a(thread.o) l
解决方法
我的精神力量说:
系统上的> g实际上是clang,默认情况下配置为使用libc.这是Mac上的常用配置.
>自制的boost分配是用libstdc编译的,也许是真正的g.
两个标准库不是二进制兼容的,因此链接器错误.要解决此问题,请将-stdlib = libstdc传递给你的clang-in-g服装,或者获得实际的GCC(Homebrew应该有一个发行版)或build boost with libc++.
Repro&演示:
$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,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> >&)",std::char_traits<char> >& std::operator<<<char,std::allocator<char> >(std::basic_ostream<char,std::char_traits<char> >&,std::basic_string<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