xamarin / xamarin-macios

.NET for iOS, Mac Catalyst, macOS, and tvOS provide open-source bindings of the Apple SDKs for use with .NET managed languages such as C#
Other
2.49k stars 515 forks source link

Static Library clang++ Error #20578

Closed dwightbennett closed 6 months ago

dwightbennett commented 7 months ago

Description

I am attempting to create a wrapper of the C library MBedTLS. I have used this library successfully in our application with a Xamarin Bindings library for over 4 years. We are porting our application to .Net Maui and started receiving a clang++ error when attempting to build iOS. Android has no problems whatsoever. I originally followed along with this tutorial when i setup the Xamarin Bindings library and it worked perfectly. I attached an example project to the ticket. The error says i am missing Undefined symbols for architecture arm64 and then a list of missing methods. When i run a lipo command on my .a file i get Architectures in the fat file: libmbedtls.a are: x86_64 arm64 arm64e So i know i have a build for arm64. When i run the command nm -g libmbedtls.a I can see that _cert_get_name does exist. I will paste the whole output below. I have a feeling that the linker for ios is linking out the library but i cannot figure out how to tell .net maui to stop linking it. I have tried turning the linker completely off but ti still gives me the error. I know i've seen other clang++ issues in the backlog and i hope this isn't a duplicate, but nothing i've tried in the other issues has fixed this build error. Thank you for any work arounds you can find or links to documentation.

Steps to Reproduce

  1. Create a .net maui Bindings library
  2. Add the libmbedtls.a file for the ios project
  3. Create the C# wrapper making sure to use the [DLLInclude("__Internal")] Attribute
  4. Add the bindings library as a project reference to your .net maui app
  5. Attempt To build the .net Maui application

Expected Behavior

.Net Maui application builds and deploys successfully.

Actual Behavior

App fails to build with a clang++ error shown in build logs

Environment

VS Code .net maui Extension: v0.10.61 (pre-release) (Also fails in non pre-release) macos Sonoma 14.4 v0.10.61 (pre-release) ``` ```

Build Logs

/usr/local/share/dotnet/packs/Microsoft.iOS.Sdk/17.2.8053/targets/Xamarin.Shared.Sdk.targets(1560,3): error : clang++ exited with code 1: [/Users/dwightbennett/depot/MauiBuildError/MauiApp1/MauiApp1.csproj::TargetFramework=net8.0-ios]
/usr/local/share/dotnet/packs/Microsoft.iOS.Sdk/17.2.8053/targets/Xamarin.Shared.Sdk.targets(1560,3): error : Undefined symbols for architecture arm64: [/Users/dwightbennett/depot/MauiBuildError/MauiApp1/MauiApp1.csproj::TargetFramework=net8.0-ios]
/usr/local/share/dotnet/packs/Microsoft.iOS.Sdk/17.2.8053/targets/Xamarin.Shared.Sdk.targets(1560,3): error :   "_cert_get_name", referenced from: [/Users/dwightbennett/depot/MauiBuildError/MauiApp1/MauiApp1.csproj::TargetFramework=net8.0-ios]
/usr/local/share/dotnet/packs/Microsoft.iOS.Sdk/17.2.8053/targets/Xamarin.Shared.Sdk.targets(1560,3): error :      -exported_symbol[s_list] command line option [/Users/dwightbennett/depot/MauiBuildError/MauiApp1/MauiApp1.csproj::TargetFramework=net8.0-ios]
/usr/local/share/dotnet/packs/Microsoft.iOS.Sdk/17.2.8053/targets/Xamarin.Shared.Sdk.targets(1560,3): error :   "_cert_get_next", referenced from: [/Users/dwightbennett/depot/MauiBuildError/MauiApp1/MauiApp1.csproj::TargetFramework=net8.0-ios]
/usr/local/share/dotnet/packs/Microsoft.iOS.Sdk/17.2.8053/targets/Xamarin.Shared.Sdk.targets(1560,3): error :      -exported_symbol[s_list] command line option [/Users/dwightbennett/depot/MauiBuildError/MauiApp1/MauiApp1.csproj::TargetFramework=net8.0-ios]
/usr/local/share/dotnet/packs/Microsoft.iOS.Sdk/17.2.8053/targets/Xamarin.Shared.Sdk.targets(1560,3): error :   "_cert_set_next", referenced from: [/Users/dwightbennett/depot/MauiBuildError/MauiApp1/MauiApp1.csproj::TargetFramework=net8.0-ios]
/usr/local/share/dotnet/packs/Microsoft.iOS.Sdk/17.2.8053/targets/Xamarin.Shared.Sdk.targets(1560,3): error :      -exported_symbol[s_list] command line option [/Users/dwightbennett/depot/MauiBuildError/MauiApp1/MauiApp1.csproj::TargetFramework=net8.0-ios]
/usr/local/share/dotnet/packs/Microsoft.iOS.Sdk/17.2.8053/targets/Xamarin.Shared.Sdk.targets(1560,3): error :   "_ctr_drbg_free", referenced from: [/Users/dwightbennett/depot/MauiBuildError/MauiApp1/MauiApp1.csproj::TargetFramework=net8.0-ios]
/usr/local/share/dotnet/packs/Microsoft.iOS.Sdk/17.2.8053/targets/Xamarin.Shared.Sdk.targets(1560,3): error :      -exported_symbol[s_list] command line option [/Users/dwightbennett/depot/MauiBuildError/MauiApp1/MauiApp1.csproj::TargetFramework=net8.0-ios]
/usr/local/share/dotnet/packs/Microsoft.iOS.Sdk/17.2.8053/targets/Xamarin.Shared.Sdk.targets(1560,3): error :   "_ctr_drbg_new", referenced from: [/Users/dwightbennett/depot/MauiBuildError/MauiApp1/MauiApp1.csproj::TargetFramework=net8.0-ios]
/usr/local/share/dotnet/packs/Microsoft.iOS.Sdk/17.2.8053/targets/Xamarin.Shared.Sdk.targets(1560,3): error :      -exported_symbol[s_list] command line option [/Users/dwightbennett/depot/MauiBuildError/MauiApp1/MauiApp1.csproj::TargetFramework=net8.0-ios]
/usr/local/share/dotnet/packs/Microsoft.iOS.Sdk/17.2.8053/targets/Xamarin.Shared.Sdk.targets(1560,3): error :   "_ctr_drbg_seed", referenced from: [/Users/dwightbennett/depot/MauiBuildError/MauiApp1/MauiApp1.csproj::TargetFramework=net8.0-ios]
/usr/local/share/dotnet/packs/Microsoft.iOS.Sdk/17.2.8053/targets/Xamarin.Shared.Sdk.targets(1560,3): error :      -exported_symbol[s_list] command line option [/Users/dwightbennett/depot/MauiBuildError/MauiApp1/MauiApp1.csproj::TargetFramework=net8.0-ios]
/usr/local/share/dotnet/packs/Microsoft.iOS.Sdk/17.2.8053/targets/Xamarin.Shared.Sdk.targets(1560,3): error :   "_debug_set_threshold", referenced from: [/Users/dwightbennett/depot/MauiBuildError/MauiApp1/MauiApp1.csproj::TargetFramework=net8.0-ios]
/usr/local/share/dotnet/packs/Microsoft.iOS.Sdk/17.2.8053/targets/Xamarin.Shared.Sdk.targets(1560,3): error :      -exported_symbol[s_list] command line option [/Users/dwightbennett/depot/MauiBuildError/MauiApp1/MauiApp1.csproj::TargetFramework=net8.0-ios]
/usr/local/share/dotnet/packs/Microsoft.iOS.Sdk/17.2.8053/targets/Xamarin.Shared.Sdk.targets(1560,3): error :   "_entropy_free", referenced from: [/Users/dwightbennett/depot/MauiBuildError/MauiApp1/MauiApp1.csproj::TargetFramework=net8.0-ios]
/usr/local/share/dotnet/packs/Microsoft.iOS.Sdk/17.2.8053/targets/Xamarin.Shared.Sdk.targets(1560,3): error :      -exported_symbol[s_list] command line option [/Users/dwightbennett/depot/MauiBuildError/MauiApp1/MauiApp1.csproj::TargetFramework=net8.0-ios]
/usr/local/share/dotnet/packs/Microsoft.iOS.Sdk/17.2.8053/targets/Xamarin.Shared.Sdk.targets(1560,3): error :   "_entropy_new", referenced from: [/Users/dwightbennett/depot/MauiBuildError/MauiApp1/MauiApp1.csproj::TargetFramework=net8.0-ios]
/usr/local/share/dotnet/packs/Microsoft.iOS.Sdk/17.2.8053/targets/Xamarin.Shared.Sdk.targets(1560,3): error :      -exported_symbol[s_list] command line option [/Users/dwightbennett/depot/MauiBuildError/MauiApp1/MauiApp1.csproj::TargetFramework=net8.0-ios]
/usr/local/share/dotnet/packs/Microsoft.iOS.Sdk/17.2.8053/targets/Xamarin.Shared.Sdk.targets(1560,3): error :   "_err_ssl_want_read", referenced from: [/Users/dwightbennett/depot/MauiBuildError/MauiApp1/MauiApp1.csproj::TargetFramework=net8.0-ios]
/usr/local/share/dotnet/packs/Microsoft.iOS.Sdk/17.2.8053/targets/Xamarin.Shared.Sdk.targets(1560,3): error :      -exported_symbol[s_list] command line option [/Users/dwightbennett/depot/MauiBuildError/MauiApp1/MauiApp1.csproj::TargetFramework=net8.0-ios]
/usr/local/share/dotnet/packs/Microsoft.iOS.Sdk/17.2.8053/targets/Xamarin.Shared.Sdk.targets(1560,3): error :   "_err_ssl_want_write", referenced from: [/Users/dwightbennett/depot/MauiBuildError/MauiApp1/MauiApp1.csproj::TargetFramework=net8.0-ios]
/usr/local/share/dotnet/packs/Microsoft.iOS.Sdk/17.2.8053/targets/Xamarin.Shared.Sdk.targets(1560,3): error :      -exported_symbol[s_list] command line option [/Users/dwightbennett/depot/MauiBuildError/MauiApp1/MauiApp1.csproj::TargetFramework=net8.0-ios]
/usr/local/share/dotnet/packs/Microsoft.iOS.Sdk/17.2.8053/targets/Xamarin.Shared.Sdk.targets(1560,3): error :   "_pk_context [/Users/dwightbennett/depot/MauiBuildError/MauiApp1/MauiApp1.csproj::TargetFramework=net8.0-ios]

Output of nm -g libmbedtls.a nm-output.txt

Example Project

Here is an example project that includes a basic .net maui app that references the bindings project. MauiBuildError.zip

it wouldnt let me just upload the .a file so i put it in a folder and compressed it here. library.zip

Not sure if its of any use but my csproj for my bindings project looks like this.

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFrameworks>net8.0-android;net8.0-ios;net8.0</TargetFrameworks>
        <SingleProject>true</SingleProject>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>

        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">11.0</SupportedOSPlatformVersion>
        <SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
    </PropertyGroup>

    <ItemGroup>
    <!-- Android libraries -->
    <None Include="Platforms\Android\libs\arm64-v8a\libmbedtls.so" Pack="true" PackagePath="runtimes\android-arm64\native" />
    <None Include="Platforms\Android\libs\armeabi-v7a\libmbedtls.so" Pack="true" PackagePath="runtimes\android-arm\native" />
    <None Include="Platforms\Android\libs\x86_64\libmbedtls.so" Pack="true" PackagePath="runtimes\android-x64\native" />

    <!-- iOS library -->
    <None Include="Platforms\iOS\libmbedtls.a" Pack="true" PackagePath="runtimes\ios\native" /> 
</ItemGroup>

<!-- <ItemGroup Condition="'$(TargetFramework)' == 'net8.0-ios'">
    <NativeReference Include="Platforms\iOS\libmbedtls.a" LinkerBehavior="LinkNone"/>
</ItemGroup> -->

<ItemGroup>
    <Folder Include="Platforms/Android/libs/" />
    <Folder Include="Platforms/Android/libs/arm64-v8a/" />
    <Folder Include="Platforms/Android/libs/armeabi-v7a/" />
    <Folder Include="Platforms/Android/libs/x86_64/" />
  </ItemGroup>

</Project>

Again thank you for your help with this issue. I'm willing to try about anything and if you need anything else i'll be happy to get you the information.

rolfbjarne commented 6 months ago
  • Add the libmbedtls.a file for the ios project

The problem is that there are two variations of the arm64 architecture, one for the simulator and one for device, and they can't both exist inside a single static library (.a file).

Your existing libmbedtls.a file has the arm64 architecture, but for device. In .NET 8, when you build for the simulator, we automatically default to building for arm64 for the simulator, but that doesn't work because the arm64 bits in the .a file aren't the right ones (legacy Xamarin didn't support arm64 in the simulator, this is why it worked there).

The proper fix is to use an xcframework, which can contain native code for all platforms and architectures Apple supports.

Here's some documentation about xcframeworks from Apple, there are also numerous other guides online:

If you got the native library from a third-party vendor, the easiest is to ask them to provide the xcframework (they'll likely have one already, because any of their customer using Xcode would have the same problem).

Some more information can be found here: https://github.com/xamarin/xamarin-macios/wiki/.NET-8-release-notes#default-runtimeidentifiers