簡體   English   中英

swig + mono:找不到庫的C#示例錯誤

[英]swig + mono : C# example errors of not finding the library

我在Mac OS X 10.6.4上使用swig 2.0.1 + mono 2.6 / 2.8。

總體構建還可以,C#示例的構建也可以。 問題是,當我運行示例(mono runme.exe)時,總是出現以下錯誤。

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

似乎找不到示例庫,但是生成的庫是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

正如寫在這個崗位 ,我跑了“MONO_LOG_LEVEL =調試單runme.exe”。 消息說,即使文件退出,mono也找不到./libexample.so。

Mono: DllImport loading library: './libexample.so'.
Mono: DllImport error loading library '(null)'.

我可以在swig / Examples中更改Makefile來解決此問題。

CFLAGS     = -arch i386

這可能是由於庫被編譯為64位引起的。 “(null)”表示Mono無法獲得此錯誤的錯誤消息。 您可以通過設置適當的編譯標志來解決此問題。 例如:

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

您也可以使用Mono的實驗性64位支持來解決此問題,但是我從未做到過,所以不確定。

System.DllNotFoundException:示例

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

您可以在界面文件中指定pinvoke簽名目標的DLL。

您還應該確保文件在動態鏈接程序搜索路徑中,即在MacOS上:

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

順便說一句,MacOS通常會期望.dylib文件,而不是.so文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM