MonoTouch System.EntryPointNotFoundException for a

2019-07-19 14:24发布

问题:

I'm trying to make a DLL with some native code functions that are accessed by my MonoTouch app. I followed the general methodology used by monotouch-bindings, where you:

  • make an xcode project and put some native code in it
  • build a static library (.a file) with xcodebuild
  • run btouch with --link-with to make a .dll file
  • add a reference to the .dll file in my MonoTouch app

.. but whenever I try to use these functions in my app, I get System.EntryPointNotFoundException. Here's code for each thing I'm trying to do:

In the .cpp file:

extern "C" {
   int SomeFunction();
}

int SomeFunction() {
   ...
}

Command line to build the .a file

xcodebuild -project MyStaticLibrary.xcodeproj -target MyStaticLibrary -sdk iphonesimulator -configuration Release clean build

The .cs file (NativeBindings.cs) with the bindings

public class MyStaticLibraryBindings
{
    [ DllImport( "__Internal" ) ]   public extern static int SomeFunction();
}

AssemblyInfo.cs for the DLL

using System;
using MonoTouch.ObjCRuntime;

[assembly: LinkWith ("libMyStaticLibrary.a", LinkTarget.Simulator | LinkTarget.ArmV7 | LinkTarget.ArmV7s, IsCxx = true, ForceLoad = true, Frameworks = "", WeakFrameworks = "")]

The command line to build the .dll

btouch -x=NativeBindings.cs AssemblyInfo.cs --out=NativeBindings.dll --link-with=libMyStaticLibrary.a,libMyStaticLibrary.a

.. the DLL builds fine, and my app sees the MyStaticLibraryBindings.SomeFunction function during compilation, but at runtime when I call it, I get System.EntryPointNotFoundException.

I have verified that libMyStaticLibrary.a does contains SomeFunction:

~/build> nm libMyStaticLibrary.a*
00000167 T _SomeFunction

回答1:

Also, the symbol found in your library is _SomeFunction while you're trying to P/Invoke SomeFunction. I had some cases where the binding was only possible with the proper prefix



回答2:

If the problem happens on a device, it's because you're building the native library for the simulator only, while you're building the dll for armv7, armv7s and the simulator. You need to build the native library 3 times, once for each targetted architecture, the lipo them together:

lipo -create -output libMyStaticLibrary.a libMyStaticLibrary-armv7.a libMyStaticLibrary-armv7s.a libMyStaticLibrary-simulator.a