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

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:

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?

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.

Related

XCode 13.1: Undefined symbol: __swift_FORCE_LOAD_$_XCTestSwiftSupport

My app was running perfectly without any errors or warnings yesterday. But when I tried to run it today, the build failed - I haven't changed anything in the code:
Undefined symbol: __swift_FORCE_LOAD_$_XCTestSwiftSupport
How to fix this, and why would something like this occur suddenly when it was working before?
And this is an issue I've been experiencing with XCode a lot lately. My code will be running smoothly without any errors, but then XCode will randomly start throwing errors when I relaunch it at a different time - without making any changes in the actual code.
I have included some photos and the full text of those photos:
InventoryApp 3 issues
Warning
Could not find or use auto-linked library 'XCTestSwiftSupport'
Could not find or use auto linked framework 'XCTest'
Error
Undefined symbol: __swift FORCE LOAD $ XCTestSwiftSupport
ld: warning: Could not find or use auto-linked library 'XCTestSwiftSupport'
ld: warning: Could not find or use auto-linked framework
'XCTest'
Undefined symbols for architecture arm64:
"__swift_FORCE_LOAD_$_XCTestSwiftSupport", referenced from:
__swift_FORCE_LOAD_$_XCTestSwiftSupport_$_inventoryApp in Button.o
__swift_FORCE_LOAD_$_XCTestSwiftSupport_$_inventoryApp in ViewModel.o
__swift_FORCE_LOAD_$_XCTestSwiftSupport_$_inventoryApp in Inventory.o
__swift_FORCE_LOAD_$_XCTestSwiftSupport_$_inventoryApp in AddView.o
__swift_FORCE_LOAD_$_XCTestSwiftSupport_$_inventoryApp in ScannerView.o
__swift_FORCE_LOAD_$_XCTestSwiftSupport_$_inventoryApp in RegisterView.o
__swift_FORCE_LOAD_$_XCTestSwiftSupport_$_inventoryApp in ContentView.o
...
(maybe you meant: __swift_FORCE_LOAD_$_XCTestSwiftSupport_$_inventoryApp)
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
This condition also occurs if you add
import XCTest
to a file in a non-test bundle.
I solved this error by navigation to target app settings > Build Phases > Link Binary With Libraries > add the linked library "XCTest.framework".
If afterwards your app starts crashing, aborting with the following errors:
dyld: Library not loaded: #rpath/XCTest.framework/Versions/A/XCTest
0_abort_with_payload
Then you can take a look at the suggestions on this thread: Xcode 5.0.2 dyld: Library not loaded: #rpath/XCTest.framework/Versions/A/XCTest
The solution that worked for me was:
Navigate to target app > Build Settings > Linking > Other Linker Flags > editing or adding -weak_framework "XCTest"
This error randomly started to occur on my Xcode project today. I changed the setting.
PROJECT -> Build Settings -> Build Options -> Enable Testing Search Paths
from No to Yes, and the build succeeded.
Edit:...
This setting is on by default if the project is created with a testing target included. In my case I believe I added the testing target later so this setting will have been false. Although, I can't say why it didn't break as soon as I added the testing target.
See:
https://xcodebuildsettings.com
None of the above worked for me. In my case, a couple of mock files were targeted to both the framework and unit testing bundle. I just unticked the framework one to get rid of this error on the inspector.
I solved this error by navigation to target app settings > Build Phases > Link Binary with Libraries > add the linked library "XCTest.framework".
The action above is totally the same as the solutions above, though the issue "Library not loaded" appeared raised.
I solved the "Library not loaded" issue by setting up the framework to be optional while linked library.
[enter image description here][1]
It works for me in Xcode 13.2.1.

linker command failed with exit code 1 when building Unity project in Xcode

Im trying to build an iOS application developed with Unity. The app builds and runs just fine on Android as an apk or aab.
When building for iOS I get no errors in Unity and the Unity-iPhone.xcodeproj and Unity-iPhone.xcworkspace are built. I open the Unity-iPhone.xcworkspace and make sure the signing of the app is well and all.
But when trying to archive the project i get these errors :
(these errors appears during the building of the UnityFramework and are associated with it)
ld: warning: Could not find or use auto-linked library 'swiftCoreMIDI'
ld: symbol(s) not found for architecture armv7
clang: error: linker command failed with exit code 1
Undefined symbol: _swiftoverride_class_getSuperclass(swift::TargetMetadata<swift::InProcess> const*)
Undefined symbol: swift::swift51override_conformsToSwiftProtocol(swift::TargetMetadata<swift::InProcess> const*, swift::TargetProtocolDescriptor<swift::InProcess> const*, llvm::StringRef, swift::TargetProtocolConformanceDescriptor<swift::InProcess> const* (*)(swift::TargetMetadata<swift::InProcess> const*, swift::TargetProtocolDescriptor<swift::InProcess> const*, llvm::StringRef))
Additionnal informations :
Im using Unity 2019.3.6f1 and I made sure to go in the iOS Resolver settings and checked the "Podfile Generation", "Auto Install Cocoapod Tools in Editor" and "Use project settings" to true as well as setting the Cocoapods Integration to "Xcode Workspace - Add Cocoapods to the Xcode workspace"
I also make sure to open the Unity-iPhone.xcworkspace and not the Unity-iPhone.xcodeproj.
I use Xcode 12.0.1 with Swift 5.3 and Im targeting iOS 10.0
My version of cocoapod is 1.8.4.
Things I have tried :
Adding the "swiftCoreMIDI" library to the list of Frameworks and Libraries and/or the list Link Binary With Libraries in the UnityFramework's build phase.
Adding DEAD_CODE_STRIPPING = NO to the build settings of the target UnityFramework.
Installing Xcode 5.0 and using it from the toolchain menu of Xcode to build.
In Unity's player settings, setting the Architecture to Universal, ARM64 and ARMv7.
Using "Symlink Unity libraries" in Unity's build options
Reduce/Increase the build target to 8.0 / 12.0
And many other things that I cant recall at the moment... (been stuck on this for days)
none of these attempts removed or changed the errors.
As I understand the problem is that a library inside the UnityFramework is trying to call some functions in the "swiftCoreMIDI" library but cannot access them. But i cant figure out why adding the library to the list of Frameworks and Libraries in the general panel and/or the list Link Binary With Libraries in the UnityFramework's build phase doesnt solve the problem.
Thanks in advance for any ideas or suggestions.
I finally found the solution, it was linked to the UnityFramework target in Xcode. I had to remove some architectures that are not concerned by my build: i386 and x86_64. In addition, I updated the Library Search Paths to include $(SDKROOT)/usr/lib/swift.
To execute those actions automatically, you can add those lines in a PostProcessBuild file in your Unity project:
var projPath = pathToBuiltProject + "/Unity-iPhone.xcodeproj/project.pbxproj";
var proj = new PBXProject();
proj.ReadFromFile(projPath);
// Update the Library Search Paths of the whole Xcode project
proj.AddBuildProperty(proj.ProjectGuid(), "LIBRARY_SEARCH_PATHS", "$(SDKROOT)/usr/lib/swift");
// Get the UnityFramework target and exclude the unwanted architectures
var unityFrameworkGuid = proj.TargetGuidByName("UnityFramework");
proj.SetBuildProperty(unityFrameworkGuid, "EXCLUDED_ARCHS", "i386");
proj.AddBuildProperty(unityFrameworkGuid, "EXCLUDED_ARCHS", "x86_64");

resolving link errors with ios workspace with static library

I've done this before, several years ago with Xcode 5 or 6. Now using Xcode 8 or 9, I seem to be missing someting.
No matter what I've done, when I try to build and link my project, I get the Linker message:
Undefined symbols for architecture arm64:
"_OBJC_CLASS_$_MyClass", referenced from:
objc-class-ref in ViewController.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
This is not a duplicate question. It is not an architecture issue.
None of the other results I've found on S-O or google resolve this issue.
I'm obviously missing some build settings, but cannot figure out which one(s).
Between build attempts I've cleaned the build folder, and deleted DerivedData. Quit and restarted Xcode. Created a new set of projects with a new workspace and walked through the steps outlined below.
I'm writing in objective-C for this project if that matters (I don't think it does)
My question is what did I forget?
What I did --
Create a project for my framework and static library
a) create the project for a cocoa-touch framework - let's call it mySDK which creates mySDK.framework
b) Add a target for a cocoa-touch static library - let's call it SDK which creates libSDK.a
c) optionally build both targets -- they build fine
d) close the project
Create a project for my application - myApp - then close the project
Create a workspace - myWorkspace -- and open the workspace
Drag both project files from #1 mySDK and #2 myApp into the workspace
Build the framework and static library (both build and create the targets successfully)
Add the static library to myApp
a) Select myApp and go to build phases
b) Open Link Binary with Libraries
c) Drag from the library build products libSDK.a into the link binaries pane, set to "Required"
The library now appears in the Frameworks group-area in the myApp
7) Select the myApp project and go to build-settings
Add to Header Search Paths = "$(SRCROOT)/../mySDK/mySDK"
This resolves compile errors in finding the headers
Now I ask, why do I need to do anything else, shouldn't Xcode figure the rest out?
Various solutions or tutorials I've reviewed suggest adding:
Other Linking Flags = -ObjC
Library Search Paths = $(BUILT_PRODUCTS_DIR)
Library Search Paths = "$(SRCROOT)/../mySDK"
Changing Skip Install from YES to NO
Installation Build Product Location = $(BUILT_PRODUCTS_DIR)
I've tried all of these in different combinations.
Once I added the library to myApp I see that the Frameworks/libSDK.a file changes from red to black when I build the library so something in the workspace/project knows I built the library and it sees it.
So why when I try build myApp, do I still see the liner error:
Undefined symbols for architecture arm64:
"_OBJC_CLASS_$_MyClass", referenced from:
objc-class-ref in ViewController.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Nothing I have done seems to clear that error. I am pulling my hair out trying to find a solution.
I have also checked that it is building all the appropriate architectures, confirmed it using 'lipo -info'
Xcode obviously knows about the library, why isn't it linking?
My app also compiled with no problem but would hit an error while linking to the static library. My library was based on C++.
I had 2 problems to fix in my case:
The headers lacked proper extern "C" declarations for C functions in a C++ file.
One .cpp file did not #include its matching header so the extern "C" would be properly applied to the compiled version.
The linker had issues because the compiler used a mangled name for the function due to the mistakes. Later confirmed this by looking in the .a file with a text editor. I found my function with "_Z11" prefixed and "v" suffixed.
There was another clue: the error was reporting the missing function name with an underscore at the front.
Undefined symbols for architecture arm64:
"_myCFunction", referenced from:
SwiftModule.swiftFunction () -> () in MySwiftClass.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Remote Display Framework

I am attempting to use Google Cast Remote Display. Simply adding the GoogleCastRemoteDisplay.framework results in linking errors. I have sorted out some, but cannot figure this out...
Anyone have an idea?
Undefined symbols for architecture armv7:
"_kvImage_ARGBToYpCbCrMatrix_ITU_R_709_2", referenced from:
l034 in GoogleCastRemoteDisplay(libCastRemoteDisplay.a-armv7-master.o)
"_vImageConvert_ARGB8888To420Yp8_Cb8_Cr8", referenced from:
l035 in GoogleCastRemoteDisplay(libCastRemoteDisplay.a-armv7-master.o)
"_vImageConvert_ARGB8888To420Yp8_CbCr8", referenced from:
l035 in GoogleCastRemoteDisplay(libCastRemoteDisplay.a-armv7-master.o)
"_vImageConvert_ARGBToYpCbCr_GenerateConversion", referenced from:
l034 in GoogleCastRemoteDisplay(libCastRemoteDisplay.a-armv7-master.o)
ld: symbol(s) not found for architecture armv7
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Solved it by running "pod install" instead of manually bringing in the .framework However this is a unity project, so there were issues with
The Unity-iPhone [Release] target overrides the HEADER_SEARCH_PATHS build setting defined in `Pods/Target Support Files/Pods/Pods.release.xcconfig'. This can lead to problems with the CocoaPods installation
went to project build settings->Other Linker Flags and added $(inherited)
Absolutely right that the Cocoapod is the best way of grabbing the library. If you were pulling it in manually thought, you'll need to link in the appropriate system frameworks, and the regular google-cast-sdk, which is dependends on - you can actually read these from the podspec even if you're not using Cocoapods. These are:
Accelerate
AudioToolbox
CoreMedia
CoreVideo
OpenGLES
QuartzCore
And Metal if using that rendering method.
The functions you listed are from the Accelerate framework.

Linking libiconv on ios

I am trying to use the objective-git library for an ios application I am in the proccess of developing. I am able to get the library to compile, link, and run perfectly fine in the simulator. However, when I try to run the application on an actual iPad, I get the followign error:
Undefined symbols for architecture armv7s:
"_libiconv", referenced from:
_git_path_iconv in libObjectiveGit-iOS.a(path.c.o)
"_libiconv_close", referenced from:
_git_path_iconv_clear in libObjectiveGit-iOS.a(path.c.o)
"_libiconv_open", referenced from:
_git_path_iconv_init_precompose in libObjectiveGit-iOS.a(path.c.o)
ld: symbol(s) not found for architecture armv7s
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I believe the issue is that I am compiling the libgit2 library with a different version of iconv then the one included with ios. However, I have messed with the build script in an attempt to force it to link with the ios version, but the error persists. How should I go about properly linking the iconv library so that I can run the application on an actual device?
You need to add libiconv.tbd to 'Link Binary With Libraries' section in Build Phases.
Note: I know ObjectiveGit fixed this issue but maybe that helps to others those who are trying to use iconv in their projects.

Resources