STD linker error with Apple LLVM 4.1

后端 未结 6 464
無奈伤痛
無奈伤痛 2020-12-04 19:49

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

相关标签:
6条回答
  • 2020-12-04 20:25

    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.

    0 讨论(0)
  • 2020-12-04 20:35

    you may also try adding an empty .cpp file to your project. This will trick xcode into loading C++ std libraries

    0 讨论(0)
  • 2020-12-04 20:39

    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.cpp
    dhcp-191:~/Development/testy/fred% nm fred.dylib | c++filt | grep dofred
    0000000000000fa0 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.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.

    0 讨论(0)
  • 2020-12-04 20:44

    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.

    0 讨论(0)
  • 2020-12-04 20:45

    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.

    0 讨论(0)
  • 2020-12-04 20:46

    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++.

    0 讨论(0)
提交回复
热议问题