dylib dynamic loading issue

MartzMartz USMember ✭✭

Hello,

I'm currently trying to load a C dylib at runtime in my iOS app and failing spectacularly. Any help appreciated.

Running in an iOS 11 simulator (so x86_64 I'd assume).

So, this

<NativeReference Include="libvlc.dylib">
      <Kind>Dynamic</Kind>
      <SmartLink>False</SmartLink>
    </NativeReference>

does not put it in the .app bundle and outputs the following warning:
/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/iOS/Xamarin.iOS.Common.targets(786,3): warning : Dynamic native references are not supported: 'libvlc.dylib'.

It isn't? docs.microsoft.com/en-us/xamarin/ios/platform/native-interop#accessing-c-methods-from-c I'm missing something here...?

Anyway, including the dylib with Content Include or BundleResource successfully includes it in the final .app bundle (even if that's not The Right Way).
P/Invoking into it throws a DllNotFoundException.
Adding <MtouchExtraArgs>--setenv:MONO_LOG_LEVEL=debug</MtouchExtraArgs> to get more info prints out a bunch of logs including

info: DllImport error loading library 'libvlc.dylib': 'dlopen(libvlc.dylib, 9): no suitable image found.  Did find:
    libvlc.dylib: no matching architecture in universal wrapper'.

But...

$ file libvlc.dylib
libvlc.dylib: Mach-O universal binary with 5 architectures: [arm_v7:current ar archive] [arm64]
libvlc.dylib (for architecture armv7):    current ar archive
libvlc.dylib (for architecture armv7s):    current ar archive
libvlc.dylib (for architecture i386):    current ar archive
libvlc.dylib (for architecture x86_64):    current ar archive
libvlc.dylib (for architecture arm64):    current ar archive

Calling ObjCRuntime.Dlfcn.dlopen with the correct path, in Main.cs, before UIApplication.Main(args, null, "AppDelegate");, always returns null.

FYI, this worked at some point a couple days ago but now I'm unsure why. Maybe planets were aligned and my bin folder had just the right pieces of artifacts hanging out together, in the right order :smile:
FYI bis, I did managed to link with the .a version of the lib before, using

<MtouchExtraArgs>-cxx -gcc_flags "-L${ProjectDir} -lbz2 -lvlc -framework AudioToolbox -framework VideoToolbox -framework AVFoundation -framework CoreMedia -framework CoreText -framework CoreImage -framework Security ${ProjectDir}/libvlc.a"</MtouchExtraArgs>

I'm on Stable (VS 7.4.1).

Any help/insights highly appreciated.

Sign In or Register to comment.