【发布时间】:2012-03-28 18:57:12
【问题描述】:
在我的系统上运行一个简单的互操作示例时遇到问题。 我构建了一个简单的 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 error loading library './libtest.so: Wrong ELF-Class: 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 位共享库?
【问题讨论】: