痛饮+单:找不到库的C#示例错误 [英] swig + mono : C# example errors of not finding the library
问题描述
我用痛饮2.0.1 +单声道在Mac OS X 10.6.4 2.6 / 2.8。
的整体建设好,且的C#示例构建还行。问题是,当我运行示例(单声道runme.exe),我总是得到以下错误。
未处理的异常:System.TypeInitializationException:一个例外是由类型初始化抛出examplePINVOKE ---> System.TypeInitializationException:一个例外是由类型初始化抛出SWIGExceptionHelper ---> System.DllNotFoundException:例如
。在(包装管理到本机)examplePINVOKE / SWIGExceptionHelper:SWIGRegisterExceptionCallbacks_example(examplePINVOKE / SWIGExceptionHelper / ExceptionDelegate,examplePINVOKE / SWIGExceptionHelper / ExceptionDelegate,examplePINVOKE / SWIGExceptionHelper / ExceptionDelegate,examplePINVOKE / SWIGExceptionHelper / ExceptionDelegate,examplePINVOKE / SWIGExceptionHelper / ExceptionDelegate,examplePINVOKE / SWIGExceptionHelper / ExceptionDelegate,examplePINVOKE / SWIGExceptionHelper / ExceptionDelegate,examplePINVOKE / SWIGExceptionHelper / ExceptionDelegate,examplePINVOKE / SWIGExceptionHelper / ExceptionDelegate,examplePINVOKE / SWIGExceptionHelper / ExceptionDelegate,examplePINVOKE / SWIGExceptionHelper / ExceptionDelegate在examplePINVOKE + SWIGExceptionHelper..cctor()
)[0x00000]中:0
---内部异常堆栈跟踪的结尾---
在examplePINVOKE..cctor()[0x00000]在0
---内部异常堆栈跟踪的结尾---
在example.gcd(的Int32 X,的Int32 Y)[0x00000]中:0
在runme.Main() [0x00000]中:0
这似乎是它不会找到实例库,但生成的库是libexample.so。
这是该库的源产生libexample.so
双富= 3.0;
INT GCD(INT X,int y)对{
:
}
这是使用libexample.so C#源
使用系统。
公共类邵仁枚
{
静态无效的主要()
{
INT X = 42;
INT Y = 105;
INT G = example.gcd(X,Y);
:
}
}
这是包装一个与SWIG生成的函数。
/ * ----------------- -------------------------------------------------- ---------
*为通过自动生成的SWIG此文件(http://www.swig.org)。
* 2.0.1版
*
*除非你知道自己在做什么,否则不要修改此文件 - 修改
*的SWIG接口文件来代替。
* --------------------------------------------- -------------------------------- * /
使用系统;使用System.Runtime.InteropServices
;
公共类例子{
公共静态INT GCD(INT X,int y)对{
INT RET = examplePINVOKE.gcd(X,Y);
返回RET;
}
公共静态双富{
集合{
examplePINVOKE.Foo_set(值);
}
获得{
双RET = examplePINVOKE.Foo_get();
返回RET;
}
}
}
这是要运行的命令,以使该库和执行
GCC -c example.c example_wrap.c
CC -bundle -undefined抑制 - flat_namespace example.o example_wrap.o -o libexample.so
进行-f ../../Makefile CSHARPSRCS ='* CS。CSHARPFLAGS =' - NOLOGO退房手续:runme.exe'csharp_compile
系膜细胞-nologo退房手续:runme.exe *的.cs
什么可能是错误的?应该怎样做,让单了解libexample.so?
ADDED
我可以使用痛饮-csharp -dllimportlibexample.soexample.i,但我得到了同样的结果。
我附上 examplePINVOKE.cs 。
由于是写在这个帖子,我跑了 MONO_LOG_LEVEL =调试单runme.exe。有消息称单找不到./libexample.so即使该文件退出
单声道:加载的DllImport库:。 /libexample.so。
单声道:错误的DllImport加载库'(空)。
SOLUTION
我可以痛饮改变Makefile文件/例子来解决这个问题
CFLAGS = -arch i386的
这可能是由该库被编译为64位造成的。的(空)是指单无法获得此错误的错误消息。您可以通过设置相应的编译标志解决这个问题。例如:
./配置CFLAGS = - Ø-arch i386的CXXFLAGS = - Ø-arch i386的LDFLAGS = -arch I386--disable依赖性跟踪
您还可以通过解决这一问题使用Mono的实验64位的支持,但我从来没有做过这么说我不知道。
I use swig 2.0.1 + mono 2.6/2.8 on Mac OS X 10.6.4.
The overall build is OK, and the build of the C# examples is also OK. The problem is that when I run the example (mono runme.exe), I always get the following errors.
Unhandled Exception: System.TypeInitializationException: An exception was thrown by the type initializer for examplePINVOKE ---> System.TypeInitializationException: An exception was thrown by the type initializer for SWIGExceptionHelper ---> System.DllNotFoundException: example at (wrapper managed-to-native) examplePINVOKE/SWIGExceptionHelper:SWIGRegisterExceptionCallbacks_example (examplePINVOKE/SWIGExceptionHelper/ExceptionDelegate,examplePINVOKE/SWIGExceptionHelper/ExceptionDelegate,examplePINVOKE/SWIGExceptionHelper/ExceptionDelegate,examplePINVOKE/SWIGExceptionHelper/ExceptionDelegate,examplePINVOKE/SWIGExceptionHelper/ExceptionDelegate,examplePINVOKE/SWIGExceptionHelper/ExceptionDelegate,examplePINVOKE/SWIGExceptionHelper/ExceptionDelegate,examplePINVOKE/SWIGExceptionHelper/ExceptionDelegate,examplePINVOKE/SWIGExceptionHelper/ExceptionDelegate,examplePINVOKE/SWIGExceptionHelper/ExceptionDelegate,examplePINVOKE/SWIGExceptionHelper/ExceptionDelegate) at examplePINVOKE+SWIGExceptionHelper..cctor () [0x00000] in :0 --- End of inner exception stack trace --- at examplePINVOKE..cctor () [0x00000] in :0 --- End of inner exception stack trace --- at example.gcd (Int32 x, Int32 y) [0x00000] in :0 at runme.Main () [0x00000] in :0
It seems like that it doesn't find the example library, but the generated library is libexample.so.
This is the source of the library to generate libexample.so
double Foo = 3.0;
int gcd(int x, int y) {
...
}
This is the C# source for using libexample.so.
using System;
public class runme
{
static void Main()
{
int x = 42;
int y = 105;
int g = example.gcd(x,y);
...
}
}
This is the wrapper function that is generated with 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;
}
}
}
This is the command to run to get the library and the execution.
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
What might be wrong? What should be done to let mono know about the libexample.so?
ADDED
I could use "swig -csharp -dllimport "libexample.so" example.i", but I got the same result. I attach the examplePINVOKE.cs.
As is written in this post, I ran "MONO_LOG_LEVEL=debug mono runme.exe". The message says mono can't find the ./libexample.so even though the file exits.
Mono: DllImport loading library: './libexample.so'. Mono: DllImport error loading library '(null)'.
SOLUTION
I could change Makefile in swig/Examples to solve this issue.
CFLAGS = -arch i386
This is likely caused by the library being compiled as 64-bit. The "(null)" means that Mono was not able to obtain the error message of this error. You can fix this by setting the appropriate compile flags. For instance:
./configure CFLAGS="-O -arch i386" CXXFLAGS="-O -arch i386" LDFLAGS="-arch i386" --disable-dependency-tracking
You may also be able to fix this by using Mono's experimental 64-bit support but I have never done that so am not sure.
这篇关于痛饮+单:找不到库的C#示例错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!