【问题标题】:swig + mono : C# example errors of not finding the libraryswig + mono:找不到库的 C# 示例错误
【发布时间】: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。

标签: c# mono swig


【解决方案1】:

这可能是由于库被编译为 64 位造成的。 “(null)”表示 Mono 无法获取此错误的错误信息。您可以通过设置适当的编译标志来解决此问题。例如:

./configure CFLAGS="-O -arch i386" CXXFLAGS="-O -arch i386" LDFLAGS="-arch i386" --disable-dependency-tracking

您也可以通过使用 Mono 的实验性 64 位支持来解决此问题,但我从未这样做过,所以不确定。

【讨论】:

  • 是的,我用 -arch i386 选项重新编译了库,它工作正常。谢谢!
【解决方案2】:

System.DllNotFoundException:示例

它似乎找不到您的非托管 dll:“示例”。

【讨论】:

  • 感谢您的回答,我详细阐述了这个问题。
【解决方案3】:

您可以在接口文件中指定 pinvoke 签名的目标 DLL。

【讨论】:

  • @iterationx : 你能给我一个接口文件的例子吗?
  • @proseek 调用 swig 时只需使用 -dllimport 指令,根据文档,swig.org/Doc2.0/SWIGDocumentation.html swig -dllimport "mydll.dll" swig.i
  • @iterationx :我的 Mac 上的 swig 没有 -dllimport 选项。还有更多提示吗?
  • @proseek 您能否详细说明您要达到的目标,我怀疑可能有更简单的方法。你能直接打开生成的c++代码并手动放入你的lib.so吗?
  • @iterationx : swig -csharp -dllimport 有效,即使结果相同。我添加到原帖。谢谢。
【解决方案4】:

您还应该确保文件位于动态链接器搜索路径中,即在 MacOS 上:

export DYLD_FALLBACK_LIBRARY_PATH="/directory/with/your/dylb/file:$DYLD_FALLBACK_LIBRARY_PATH"

顺便说一句,MacOS 通常需要 .dylib 文件,而不是 .so 文件。

【讨论】:

  • 我添加了环境变量,但它似乎不起作用。对于 .so,swig 生成 .so 而不是 .dylib,如下所示“cc -bundle -undefined suppress -flat_namespace example.o example_wrap.o -o libexample.so”。感谢您的帮助。
  • 我看到应该设置变量来解决其他问题。 - stackoverflow.com/questions/3918410/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多