STD linker error with Apple LLVM 4.1 STD linker error with Apple LLVM 4.1 xcode xcode

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.


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


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.