【发布时间】:2011-04-23 20:25:17
【问题描述】:
我在 Mac OS X 10.6.4 上使用 swig 2.0.1 + mono 2.6/2.8。
整体构建还可以,C#示例的构建也可以。问题是当我运行示例(mono runme.exe)时,总是出现以下错误。
未处理的异常:System.TypeInitializationException:类型初始值设定项引发了异常,例如PINVOKE ---> System.TypeInitializationException:SWIGExceptionHelper 的类型初始值设定项引发了异常 ---> System.DllNotFoundException:示例 在(包装器托管到本机)示例PINVOKE/SWIGExceptionHelper:SWIGRegisterExceptionCallbacks_example (examplePINVOKE/SWIGExceptionHelper/ExceptionDelegate,examplePINVOKE/SWIGExceptionHelper/ExceptionDelegate,examplePINVOKE/SWIGExceptionHelper/ExceptionDelegate,examplePINVOKE/SWIGExceptionHelper/ExceptionDelegate,examplePINVOKE/SWIGExceptionHelper/ExceptionDelegate,examplePINVOKE/SWIGException ,示例PINVOKE/SWIGExceptionHelper/ExceptionDelegate,示例PINVOKE/SWIGExceptionHelper/ExceptionDelegate,示例PINVOKE/SWIGExceptionHelper/ExceptionDelegate,示例PINVOKE/SWIGExceptionHelper/ExceptionDelegate,示例PINVOKE/SWIGExceptionHelper/ExceptionDelegate) 例如PINVOKE+SWIGExceptionHelper..cctor () [0x00000] in :0 --- 内部异常堆栈跟踪结束 --- 例如PINVOKE..cctor () [0x00000] in :0 --- 内部异常堆栈跟踪结束 --- 在 example.gcd (Int32 x, Int32 y) [0x00000] in :0 在 runme.Main () [0x00000] in :0好像没有找到示例库,但是生成的库是libexample.so。
这是生成libexample.so的库的来源
double Foo = 3.0;
int gcd(int x, int y) {
...
}
这是使用 libexample.so 的 C# 源代码。
using System;
public class runme
{
static void Main()
{
int x = 42;
int y = 105;
int g = example.gcd(x,y);
...
}
}
这是使用 SWIG 生成的包装函数。
/* ----------------------------------------------------------------------------
* This file was automatically generated by SWIG (http://www.swig.org).
* Version 2.0.1
*
* Do not make changes to this file unless you know what you are doing--modify
* the SWIG interface file instead.
* ----------------------------------------------------------------------------- */
using System;
using System.Runtime.InteropServices;
public class example {
public static int gcd(int x, int y) {
int ret = examplePINVOKE.gcd(x, y);
return ret;
}
public static double Foo {
set {
examplePINVOKE.Foo_set(value);
}
get {
double ret = examplePINVOKE.Foo_get();
return ret;
}
}
}
这是运行以获取库和执行的命令。
gcc -c example.c example_wrap.c cc -bundle -undefined suppress -flat_namespace example.o example_wrap.o -o libexample.so make -f ../../Makefile CSHARPSRCS='*.cs' CSHARPFLAGS='-nologo -out:runme.exe' csharp_compile gmcs -nologo -out:runme.exe *.cs可能出了什么问题?应该怎么做才能让 mono 知道 libexample.so?
添加
我可以使用 "swig -csharp -dllimport "libexample.so" example.i",但我得到了相同的结果。 我附上examplePINVOKE.cs。
正如post 中所写,我运行了“MONO_LOG_LEVEL=debug mono runme.exe”。消息说即使文件退出,mono 也找不到 ./libexample.so。
Mono:DllImport 加载库:'./libexample.so'。 单声道:DllImport 错误加载库“(空)”。解决方案
我可以在 swig/Examples 中更改 Makefile 来解决这个问题。
CFLAGS = -arch i386【问题讨论】:
-
您发布了包装器,但没有发布实际的 P/Invoke 定义。 PINVOKE 类的例子在哪里?
-
@mhutch :我添加了examplePINVOKE.cs,谢谢。
-
对于不同的库,我遇到了与此非常相似的错误。还有
DllImport error loading library '(null)'。不过我使用的是 Mono 2.8。