提问
我在系统上运行一个简单的互操作示例时遇到问题.我建立了一个简单的32位共享库,称为libtest.so(c)
g++ -c -fpic test.cpp -m32
g++ -shared -fpic -o libtest.so test.o -m32
我的系统:
Ubuntu Linux 10.04 x86_64
Mono C#编译器版本2.4.4.0
另外,我有一个使用共享库的示例C#程序:
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
public class Test
{
[DllImport("libdl.so")]
static extern IntPtr dlopen(string filename, int flags);
[DllImport("libdl.so")]
static extern IntPtr dlclose(IntPtr handle);
[DllImport ("./libtest.so")]
private static extern void HelloWorld();
[DllImport ("./libtest.so",EntryPoint="Test")]
private static extern int Testl(int a,int b);
public static int Main(string[] args)
{
IntPtr handle = dlopen("./libtest.so",2);
if(handle == IntPtr.Zero)
{
Console.WriteLine("Error loading shared library");
return -1;
}
HelloWorld();
int ret = Testl(116,1);
Console.WriteLine("Result from shared-Librarry Call: " + ret);
dlclose(handle);
return 0;
}
}
问题:加载库不起作用.
导出MONO_LOG_LEVEL = debug给我以下提示:
Mono-INFO:DllImport错误加载库’./libtest.so:错误的ELF类:ELFCLASS32′.
好吧,我想mono在64位模式下运行我的程序,因此它不能调用32位共享库?如果我以64位模式(不带-m32)构建共享库,那么一切正常!
我的Mono-Compiler 2.4.4.没有选择使用/ platform:x86指定平台的选项,因此我安装了2.10版,但是使用它也不起作用.
/opt/mono-2.10/bin/gmcs /platform:x86 sharpCall.cs
是否可以在64位系统上加载32位共享库?
最佳答案
问题是您的系统上安装了64位版本的Mono,它只能P / Invoke进入64位本机库,而不能P / Invoke进入32位本机库.-platform:x86标志用于C#编译器,而不用于运行时,并且不暗示运行时使用32位内存空间.
如果要P / Invoke到32位本机库中,则需要在Ubuntu系统上安装Mono的32位版本.