xamarin/xamarin-macios

DllImport(__Internal) causes DllNotFoundException

Closed this issue · 7 comments

Hi, I am trying call C function which linked staticly to project but it fails. I am trying to build on Windows with connection to Mac Apple Silicon M1

Steps to Reproduce

  1. Create .NET Maui Project
  2. Link C library staticly
  3. Write DllImport("__Internal")

Expected Behavior

It should throw entry point not found if it can't find it

Actual Behavior

It throws DllNotFoundException

Environment

Windows x64 and Mac Apple Silicon M1

Here is the github link

Your native library declares the function main, and your C# code tries to call it.

This won't work, because there's already a native main function in the executable, which will be called instead of your native main function (and this ends up crashing the app).

Additionally, the case is wrong for the location of the native library in the NuGet, instead of:

runtimes/iOS-arm64/native/libPraticadNative.a

it must be:

runtimes/ios-arm64/native/libPraticadNative.a

This way we'll automatically find and link with the native library (there's no need to add it to the NativeLib item group, which we don't read anyways, so it has no effect).

Can you try that and see if it works?

It still crashes. I renamed the function to SubMain (both in wrapper and library) and it still throws DllNotFoundException. Also already fixed targets file. So it shouldn't be a problem.

It still crashes. I renamed the function to SubMain (both in wrapper and library) and it still throws DllNotFoundException. Also already fixed targets file. So it shouldn't be a problem.

Can you update your test repo with your changes?

@rolfbjarne Yes, you can checkout updated version now.

Turns out I need to disable hot restart. But I don't why I need to do that...

Turns out I need to disable hot restart. But I don't why I need to do that...

Hot Restart doesn't support static libraries: https://github.com/dotnet/docs-maui/blob/main/docs/ios/hot-restart.md#limitations

This is because using a static library requires a native linker (to link the static library into the main executable), and the native linker (which comes from Xcode's toolchain) is only available on macOS.

Closing as working as designed.