Monday, February 22, 2016

STD linker error with Apple LLVM 4.1

STD linker error with Apple LLVM 4.1


I've got a large static library in C++ with bits of Objective-C originally built for iOS (armv7).

I built a OS X (64-bit Intel x86_64) version of it, but as soon as I tried to use it in a OS X app project (targeted to Lion 10.7), tens of linker errors appeared, most of them about standard library symbols.

I know how to solve "my" linker problems, but the STD ones copied below are bugging me.

"std::basic_filebuf >::is_open() const"  "std::basic_stringbuf, std::allocator >::str() const"  "std::basic_ios >::widen(char) const"  "std::istream& std::istream::_M_extract(double&)"  "std::ostream::put(char)"  "std::ostream::flush()"  "std::ostream& std::ostream::_M_insert(void const*)"  "std::ostream& std::ostream::_M_insert(bool)"  "std::ostream& std::ostream::_M_insert(double)"  "std::ostream& std::ostream::_M_insert(unsigned long)"  "std::ostream::operator<<(int)"  "std::ostream::operator<<(short)"  "std::string::_Rep::_M_destroy(std::allocator const&)"  "std::string::_Rep::_S_terminal"  "std::string::_Rep::_S_empty_rep_storage"  "std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator const&)"  "std::string::append(char const*, unsigned long)"  "std::string::append(std::string const&)"  "std::string::assign(std::string const&)"  "std::string::reserve(unsigned long)"  "std::basic_string, std::allocator >::basic_string(char const*, std::allocator const&)"  "std::basic_string, std::allocator >::basic_string(std::string const&)"  "std::basic_string, std::allocator >::~basic_string()"  "std::basic_ofstream >::open(char const*, std::_Ios_Openmode)"  "std::basic_ofstream >::close()"  "std::basic_ofstream >::basic_ofstream()"  "std::basic_ofstream >::~basic_ofstream()"  "std::_List_node_base::hook(std::_List_node_base*)"  "std::_List_node_base::unhook()"  "std::basic_stringstream, std::allocator >::basic_stringstream(std::string const&, std::_Ios_Openmode)"  "std::basic_stringstream, std::allocator >::basic_stringstream(std::_Ios_Openmode)"  "std::basic_stringstream, std::allocator >::~basic_stringstream()"  "std::basic_ostringstream, std::allocator >::basic_ostringstream(std::_Ios_Openmode)"  "std::basic_ostringstream, std::allocator >::~basic_ostringstream()"  "std::ios_base::Init::Init()"  "std::ios_base::Init::~Init()"  "std::basic_ios >::clear(std::_Ios_Iostate)"  "std::basic_ostream >& std::__ostream_insert >(std::basic_ostream >&, char const*, long)"  "std::_Rb_tree_decrement(std::_Rb_tree_node_base*)"  "std::_Rb_tree_increment(std::_Rb_tree_node_base const*)"  "std::_Rb_tree_increment(std::_Rb_tree_node_base*)"  "std::__throw_logic_error(char const*)"  "std::__throw_length_error(char const*)"  "std::__throw_out_of_range(char const*)"  "std::_Rb_tree_rebalance_for_erase(std::_Rb_tree_node_base*, std::_Rb_tree_node_base&)"  "std::_Rb_tree_insert_and_rebalance(bool, std::_Rb_tree_node_base*, std::_Rb_tree_node_base*, std::_Rb_tree_node_base&)"  "std::cerr"  "std::cout"  

I checked my build settings, my project links to the standard library (-stdlib=libc++) and I can use std::cout without any problem in my main.cpp.

I changed the compiler in the build settings from Apple LLVM 4.1 to LLVM GCC 4.2 and the problem disappeared. I want to keep using Apple LLVM 4.1. How can I fix that?

Thanks!

Answer by Petesh for STD linker error with Apple LLVM 4.1


Change the standard library that is linked to use libstdc++ instead of libc++ - the problem is that the other library was compiled using the g++ mode which uses the libstdc++ library.

Consider the following sample code:

dhcp-191:~/Development/testy/fred% cat fred.cpp  #include   #include   #include "fred.h"    using namespace std;    bool dofred(string &x)  {      cout << x << endl;      return true;  }  dhcp-191:~/Development/testy/fred% cat fred.h    #include   #include     bool dofred(std::string &x);    dhcp-191:~/Development/testy/fred% clang++ -stdlib=libc++ -shared -o fred.dylib fred.cpp  dhcp-191:~/Development/testy/fred% nm fred.dylib | c++filt | grep dofred  0000000000000fa0 T dofred(std::__1::basic_string, std::__1::allocator >&)  dhcp-191:~/Development/testy/fred% clang++ -stdlib=libstdc++ -shared -o fred.dylib fred.cpp  dhcp-191:~/Development/testy/fred% nm fred.dylib | c++filt | grep dofred                       0000000000000e30 T dofred(std::string&)  

You get two completely different exported symbols. When trying to use the symbol, the app that uses the same -stdlib flag will be able to link, while the app that doesn't will display a link error.

Answer by jlukanta for STD linker error with Apple LLVM 4.1


I had this problem after putting all C++ files into a separate library. I did set the settings of all projects to use libc++, but the linker does not link with libc++. If I add a C++ file to the main project, the problem would disappear. To fix this, you can add '-lc++' on the "Other Linker Flags" section of the main project. This would force XCode to link to libc++.

EDIT: As the other poster said, XCode may be behaving correctly. I had expected it to know to add C++ linkage because the C++ lib source code is on the same workspace.

Answer by user2073196 for STD linker error with Apple LLVM 4.1


In response to jlukanta: I had the same problem. I have been careful to choose the right STD but I still got those errors. But that's not a bug, it actually makes sense: Why should Xcode link with the c++ stdlib if you don't have any C++ code in your project?

Of course, this is a problem when you don't have C++ code in your project but still C++ libraries.

Answer by gzfrancisco for STD linker error with Apple LLVM 4.1


In iOS 7 I use a library for charts and have the same issue. In this case lib stdc++ does not resolve the issue.

I add the stdc++.6.dylib to my build phase and the symbols are found.

Answer by djacobs7 for STD linker error with Apple LLVM 4.1


I just had a similar problem, and I had to go to "Build Settings" and then "Apple LLVM 5.1 - Language - C++" and then change the "C++ Standard Library" to libstdc++.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

No comments:

Post a Comment