Swig with MonoTouch / Xamarin.iOS TypeInitializationException / EntryPointNotFoundException

时间:2013-10-31 21:17:30

标签: visual-studio-2012 xamarin.ios swig

我试图使用SWIG将非托管C ++包装到我的Xamarin.iOS项目中。在本教程之后,我已成功将其包装到C#项目中: https://stackoverflow.com/a/9816646/2494785

一旦我尝试合并MonoTouch,一旦调用涉及我的C ++代码,我总是会遇到运行时错误。

我理解在使用SWIG和Monotouch时需要进行一些小调整,但我必须遗漏一些东西。我正在关注Stephane的博客http://blog.reblochon.org/2013/01/c-bindings-for-monotouch-using-swig.html#more作为参考。

我的C#/ Monotouch项目之间的差异:

的MonoTouch

  • swig -csharp -c ++ -outdir myPath -dllimport __Internal cpp_file.i
  • 然后我在我的cppPINVOKE.cs上运行this ruby​​脚本(由James Moore创建)

    ARGF.each do |l|
       case l
       when /public delegate void Exception(Argument)?Delegate/
          puts "  [MonoTouch.MonoNativeFunctionWrapper]", l
       when /static void SetPendingArgument(Null|OutOfRange)?Exception/
          puts "  [MonoTouch.MonoPInvokeCallback (typeof (ExceptionArgumentDelegate))]", l
       when /static void SetPending.*Exception/
          puts "  [MonoTouch.MonoPInvokeCallback (typeof (ExceptionDelegate))]", l
       when /static string CreateString/
          puts "  [MonoTouch.MonoPInvokeCallback (typeof (SWIGStringDelegate))]", l
       else
          puts l
       end
    end
    

C#

  • swig -csharp -c ++ -outdir myPath cpp_file.i

这是我尝试运行项目时遇到的异常:

  

未处理的例外:

     

System.TypeInitializationException:抛出异常   cppPINVOKE的类型初始值设定项

     

未处理的异常:System.TypeInitializationException:异常   由cppPINVOKE的类型初始化程序抛出--->   System.TypeInitializationException:抛出异常   SWIGExceptionHelper的类型初始值设定项--->   System.EntryPointNotFoundException:SWIGRegisterExceptionCallbacks_cpp   at(包装器托管到原生)   cppPINVOKE / SWIGExceptionHelper:SWIGRegisterExceptionCallbacks_cpp   (cppPINVOKE / SWIGExceptionHelper / ExceptionDelegate,cppPINVOKE / SWIGExceptionHelper / ExceptionDelegate,cppPINVOKE / SWIGExceptionHelper / ExceptionDelegate,cppPINVOKE / SWIGExceptionHelper / ExceptionDelegate,cppPINVOKE / SWIGExceptionHelper / ExceptionDelegate,cppPINVOKE / SWIGExceptionHelper / ExceptionDelegate,cppPINVOKE / SWIGExceptionHelper / ExceptionDelegate,cppPINVOKE / SWIGExceptionHelper / ExceptionDelegate,cppPINVOKE / SWIGExceptionHelper / ExceptionDelegate,cppPINVOKE / SWIGExceptionHelper / ExceptionDelegate,cppPINVOKE / SWIGExceptionHelper / ExceptionDelegate)   at cppPINVOKE + SWIGExceptionHelper..cctor()[0x000ef] in   c:\ Users \ joshs \ Documents \ Visual Studio   2012 \项目\ StackOverFlowSwigWithXamarin \ StackOverFlowSwigWithXamarin \生成\ cppPINVOKE2.cs:121   ---内部异常堆栈跟踪结束---在c:\ Users \ joshs \ Documents \ Visual Studio中的cppPINVOKE..cctor()[0x00000]   2012 \项目\ StackOverFlowSwigWithXamarin \ StackOverFlowSwigWithXamarin \生成\ cppPINVOKE2.cs:141   ---内部异常堆栈跟踪结束---在c:\ Users \ joshs \ Documents \ Visual Studio中的cpp_file..ctor()[0x00000]   2012 \项目\ StackOverFlowSwigWithXamarin \ StackOverFlowSwigWithXamarin \生成\ cpp_file.cs:43   在StackOverFlowSwigWithXamarin.MyViewController.b__0   (System.Object sender,System.EventArgs e)[0x00001] in   c:\ Users \ joshs \ Documents \ Visual Studio   2012 \项目\ StackOverFlowSwigWithXamarin \ StackOverFlowSwigWithXamarin \ MyViewController.cs:38   在MonoTouch.UIKit.UIControlEventProxy.Activated()[0x00000]中   /Developer/MonoTouch/Source/monotouch/src/UIKit/UIControl.cs:30 at   (包装器托管到原生)MonoTouch.UIKit.UIApplication:   UIApplicationMain(int,string [],intptr,intptr)at   MonoTouch.UIKit.UIApplication.Main(System.String [] args,   System.String principalClassName,System.String delegateClassName)   [0x0004c] in   /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
  在StackOverFlowSwigWithXamarin.Application.Main(System.String []   args)[0x00001]在c:\ Users \ joshs \ Documents \ Visual Studio中   2012 \项目\ StackOverFlowSwigWithXamarin \ StackOverFlowSwigWithXamarin \ Main.cs:17   2013-10-31 14:45:48.261 StackOverFlowSwigWithXamarin [13645:21e03]   未处理的托管异常:类型抛出异常   用于cppPINVOKE(System.TypeInitializationException)的初始化程序   cpp_file..ctor()[0x00000]在c:\ Users \ joshs \ Documents \ Visual Studio中   2012 \项目\ StackOverFlowSwigWithXamarin \ StackOverFlowSwigWithXamarin \生成\ cpp_file.cs:43   在StackOverFlowSwigWithXamarin.MyViewController.b__0   (System.Object sender,System.EventArgs e)[0x00001] in   c:\ Users \ joshs \ Documents \ Visual Studio   2012 \项目\ StackOverFlowSwigWithXamarin \ StackOverFlowSwigWithXamarin \ MyViewController.cs:38   在MonoTouch.UIKit.UIControlEventProxy.Activated()[0x00000]中   /Developer/MonoTouch/Source/monotouch/src/UIKit/UIControl.cs:30 at   (包装器管理到本机)   MonoTouch.UIKit.UIApplication:UIApplicationMain   (int,string [],intptr,intptr)在MonoTouch.UIKit.UIApplication.Main   (System.String [] args,System.String principalClassName,System.String   / Developer /中的delegateClassName)[0x0004c]   MonoTouch / Source / monotouch / src / UIKit / UIApplication.cs:38 at   StackOverFlowSwigWithXamarin.Application.Main(System.String [] args)   [0x00001]在c:\ Users \ joshs \ Documents \ Visual Studio中   2012 \项目\ StackOverFlowSwigWithXamarin \ StackOverFlowSwigWithXamarin \ Main.cs:17   mono-rt:Stacktrace:

那么为什么我会收到此异常,我该如何解决?


编辑(2013-11-1) 我的(更多/更少)完整设置:

VS2012 StackOverFlowSwigWithXamarin.sln包含两个项目

  • cpp.vcxproj(C ++项目)
  • StackOverFlowSwigWithXamarin.csproj(从VS创建的Xamarin iOS项目(即No .xibs))
    • Xamarin项目是在VS2012文件中制作的 - >新 - >项目...... - > Visual C# - > iOS - > iPad - > HelloWorld应用程序

cpp Project

cpp_file.h

#pragma once

#ifdef cpp_EXPORTS
#define cpp_API __declspec(dllexport)
#else
#define cpp_API __declspec(dllimport)
#endif

class cpp_API cpp_file
{
public:
    cpp_file(void);
    ~cpp_file(void);

    int times2(int arg);
};

cpp_file.cpp

#include "cpp_file.h"
cpp_file::cpp_file(void)
{
}
cpp_file::~cpp_file(void)
{
}
int cpp_file::times2(int arg)
{
    return arg * 2;
}

cpp_file.i

%module cpp

%{
#include "cpp_file.h"
%}

%include <windows.i>
%include "cpp_file.h"

在VS2012中,cpp_file.i文件设置为自定义构建选项。它运行命令

  

swig -csharp -c ++ -outdir&#34; $(SolutionDir)StackOverFlowSwigWithXamarin \ Generated&#34; cpp_file.i

自定义构建工具中的输出指定为:

  

cpp_file_wrap.cxx

....但我从不使用这个文件,我不知道它的用途是什么?

构建我的cpp项目会生成以下文件:

  • cpp_file_wrap.cxx
  • cpp.cs
  • cpp_file.cs
  • cppPINVOKE.cs

之后,我运行我的ruby脚本来创建一个名为 cppPINVOKE2.cs

的新文件

StackOverFlowSwigWithXamarin项目

的AssemblyInfo.cs

我注意到Stephane提到添加[assembly: LinkWith(....),但我没有,因为我从未创建过.lib / .a

自动生成会添加到我的项目中:

  • cpp.cs
  • cpp_file.cs
  • cppPINVOKE2.cs(来自ruby脚本,如上所述)

AppDelegate.cs,Main.cs

我没有更改默认的VS2012 - &gt; iPad HelloWorldApplication

MyViewController.cs

我在默认的HelloWorldApplication中添加了一行。当用户按下屏幕上的按钮时,它会尝试实例化cpp_file()

我的疑虑

  • 我从不触及.cxx文件。为什么会这样? (也许这是我问题的一部分)
  • 从Stephane的博客中,他建议将[assembly: Linkwith(...)]添加到我的assemblyInfo.cs中。我从来没有创建过图书馆,我应该有吗?如果是,那是什么?

0 个答案:

没有答案