Search code examples
dependency-injectionxamarin.ioslinkernativestatic-linking

Xamarin.iOS native linking error MTOUCH MT5210 undefined symbol std::string


My Xamarion.iOS binding project has the links to all the required native video SDK libraries and native OS frameworks. I have set the required frameworks using Frameworks attribute. Other settings include SmartLink=true, ForceLoad=true, IsCxx=true, LinkerFlags="-lc++". It can be built into a C# DLL. In iOS project, the DLL is referenced and used. However there are native linking errors.

MTOUCH: Error MT5210: Native linking failed, undefined symbol: std::string::_Rep::_M_destroy(std::allocator<char> const&). Please verify that all the necessary frameworks have been referenced and native libraries are properly linked in. 

The details in build log:

> Undefined symbols for architecture arm64:
>       "std::string::_Rep::_M_destroy(std::allocator<char> const&)", referenced from:
>           HPR_UTF82A(char const*) in libHCCore.a(HPR_Utils.o)
>           HPR_A2UTF8(char const*) in libHCCore.a(HPR_Utils.o)
>       "std::string::_Rep::_S_empty_rep_storage", referenced from:
>           HPR_UTF82A(char const*) in libHCCore.a(HPR_Utils.o)
>           HPR_A2UTF8(char const*) in libHCCore.a(HPR_Utils.o)
>       "std::string::assign(char const*, unsigned long)", referenced from:
>           HPR_UTF82A(char const*) in libHCCore.a(HPR_Utils.o)
>           HPR_A2UTF8(char const*) in libHCCore.a(HPR_Utils.o)
>       "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char>
> const&)", referenced from:
>           HPR_A2UTF8(char const*) in libHCCore.a(HPR_Utils.o)
>       "std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::string const&)", referenced
> from:
>           HPR_A2UTF8(char const*) in libHCCore.a(HPR_Utils.o)
>     ld: symbol(s) not found for architecture arm64
>     clang : error : linker command failed with exit code 1 (use -v to see invocation)

The wrap errors:

MTOUCH: Warning MT5215: References to 'System' might require additional -framework=XXX or -lXXX instructions to the native linker (MT5215)

Error MT5201: Native linking failed. Please review the build log and the user flags provided to gcc: -ObjC (MT5201)

MTOUCH: Error MT5202: Native linking failed. Please review the build log. (MT5202)

clang: Error: linker command failed with exit code 1 (use -v to see invocation)

Please shed light. Thanks.

No such native linking error in Xcode. The build settings:

enter image description here


Solution

  • See https://github.com/xamarin/xamarin-macios/issues/8669.

    We understand that the issue with our iOS app is caused by the mix of libstdc++ and libc++ libraries of our vendor's SDK. Their libHCCore.a was compiled with libstdc++ while all the rest were compiled with libc++. I have tried additional linking options in Visual Studio for Mac, but failed with “libstdc++ is deprecated” and “Library –lstdc++ is not found”. Here is Microsoft Sebastien Pouliot's advice:

    I think the issue is one of the native libraries libHCCore.a is compiled with libstdc++ which caused the error undefined symbol std::string while the rest are libc++. You can't mix both. Building with libstdc++ was deprecated with Xcode 8 and is not supported in Xcode 10 when targeting iOS. C++ projects must now migrate to libc++ and are recommended to set a deployment target of macOS 10.9 or later, or iOS 7 or later. ... Project dependencies such as static archives that were built against libstdc++ will also need to be rebuilt against libc++. https://developer.apple.com/documentation/xcode_release_notes/xcode_10_release_notes?language=objc You'll need to talk with your vendor to get an updated that is built against libc++ Please rebuild libHCCore.a with libc++.

    This issue has been resolved by rebuilding libHCCore.a with libc++ through our vendor.