问题
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 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 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, std::allocator >::basic_string(char const*, std::allocator const&)", referenced from: HPR_A2UTF8(char const*) in libHCCore.a(HPR_Utils.o) "std::basic_string, std::allocator >::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:
回答1:
Managed linking and native linking may remove code. Link All may remove more code. So need to solve the problem instead of making the issues disappear using Link All. When using Link All, need to use Preserve attribute to preserve code.
I added all native references, set the linking options correctly. Also I set the linker flags to include all the required OS platform frameworks correctly. After adding reference to a native dynamic framework, the error disappeared. Then instead of getting undefined symbols, I got duplicate symbols. I had to remove a reference to a native library.
Although the iOS project is linked and built after referencing dynamic frameworks, there is a runtime issue. Our SDK provider confirmed that we must not use their dynamic frameworks. The dynamic frameworks are provided for other customers for a different target SimpleDemo_Dy in Xcode. They are older versions SDK. We should just use the static libraries.
I have re-examined the Xcode build log. No dynamic frameworks are used for target SimpleDemo in Xcode. The question is why Xcode project builds without using any dynamic frameworks, but Visual Studio for Mac build fails with a native linking error?
I’ve created a sample iOS app which only uses static libraries. It builds and runs OK. I've shared the sample iOS app with you. The link is https://drive.google.com/file/d/10QXkaZE7lURN6IF96NIrTGY2lmn88ctF/view?usp=sharing.
I haven’t been able to achieve the same in our product app with identical binding project settings. I even tried to swap the binding project from the sample app. It didn't help. Also I compared the iOS project settings from project options UI and WinMerge. I still couldn't find what's wrong. I have tried clean and rebuild many times. I have tried to check out to a fresh workspace. It still has undefined symbol error.
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++.
Adding Linker Flags -lstdc++ to the native reference didn't help. How to used additional Mtouch arguments in iOS project or additional btouch arguments in binding project to resolve the issue?
来源:https://stackoverflow.com/questions/61903746/xamarin-ios-native-linking-error-mtouch-mt5210-undefined-symbol-stdstring