我已经运行在我的系统一个简单的例子互操作的问题。 我建立了一个叫libtest.so(C ++)
i have a problem running a simple interop example on my system. I built a simple 32-bit shared library called libtest.so (c++)
g++ -c -fpic test.cpp -m32
g++ -shared -fpic -o libtest.so test.o -m32
我的系统: Ubuntu Linux操作系统的10.04 x86_64的
My System: Ubuntu Linux 10.04 x86_64
单C#编译器版本2.4.4.0
Mono C# compiler version 2.4.4.0
另外,我有一个示例C#程序中使用我的共享库:
In addition i have a sample c# program using my shared library:
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;
}
}
的问题:加载库无法正常工作
The Problem: Loading the library does not work.
导出MONO_LOG_LEVEL =调试给了我以下提示: 单-INFO:的DllImport加载库'./libtest.so:错误的ELF级:ELFCLASS32。
exporting MONO_LOG_LEVEL=debug gives me the following hint: Mono-INFO: DllImport error loading library './libtest.so: Wrong ELF-Class: ELFCLASS32'.
嗯,我想单运行我的程序在64位模式,因此不能调用32位的共享库?如果我在64位模式构建的共享库(不-m32)一切工作正常!
Well i guess mono runs my program in 64-bit mode and therefore it cannot call a 32-bit shared library? If i build the shared library in 64 bit mode (without -m32) everything works fine!!
我的单编译器2.4.4。不必指定与/平台的平台选项:86,所以我安装了2.10版,但使用它也不行
My Mono-Compiler 2.4.4. does not have the option to specify the platform with /platform:x86 and therefore i installed version 2.10, but using it does not work either.
/opt/mono-2.10/bin/gmcs /platform:x86 sharpCall.cs
是否有可能加载在64位系统的32位共享库?
Is there a possibility to load 32-bit shared libraries on a 64-bit system?
现在的问题是,你有单声道的64位版本的系统上安装的可的只有的P /调用到64位机库,它的不能的P /调用到32位机库。
The problem is that you have a 64bit version of Mono installed on your system which can only P/Invoke into 64bit native libraries, it cannot P/Invoke into 32bit native libraries.
的-platform:86标志是指对于C#的编译的,未运行时,不暗示到运行时使用32位的存储器空间
The -platform:x86 flag is meant for the C# compiler, not the runtime, and does not hint to the runtime to use a 32bit memory space.
您需要,如果你想你的Ubuntu系统上安装Mono的32位版本的P / Invoke到32位机库。
You need to install the 32bit version of Mono on your Ubuntu system if you want to P/Invoke into 32bit native libraries.