mono and unmanaged code in ubuntu

╄→尐↘猪︶ㄣ 提交于 2019-12-06 08:59:06

问题


I'm using Mono 2.10 running on Ubuntu 12 x64. Now I need to know how to use unmanaged code.

[DllImport("libc.so")]
public static extern int getpid ();

And, in the next step I have:

getpid();

and I recieve this errorL

$ MONO_LOG_LEVEL="debug" MONO_LOG_MASK="dll" mono libc_test.exe
Mono: DllImport attempting to load: 'libc.so'.
Mono: DllImport loading library:     '/home/ibaranov/Documents/MONO/libc_test/libc_test/bin/Debug/libc.so'.
Mono: DllImport error loading library '/home/ibaranov/Documents/MONO/libc_test/libc_test/bin/Debug/libc.so: cannot open shared     object file: No such file or directory'.
Mono: DllImport loading library:     '/home/ibaranov/Documents/MONO/libc_test/libc_test/bin/Debug/libc.so'.
Mono: DllImport error loading library     '/home/ibaranov/Documents/MONO/libc_test/libc_test/bin/Debug/libc.so: cannot open shared     object file: No such file or directory'.
Mono: DllImport loading location: 'libc.so'.
Mono: DllImport error loading library: '/usr/lib/x86_64-linux-gnu/libc.so: invalid     ELF header'.
Mono: DllImport loading location: 'libc.so'.
Mono: DllImport error loading library: '/usr/lib/x86_64-linux-gnu/libc.so: invalid     ELF header'.
Mono: DllImport loading: 'libc.so'.
Mono: DllImport error loading library '/usr/lib/x86_64-linux-gnu/libc.so: invalid     ELF header'.
Mono: DllImport unable to load library '/usr/lib/x86_64-linux-gnu/libc.so: invalid     ELF header'.
Mono: DllImport attempting to load: 'libc.so'.
Mono: DllImport loading library:     '/home/ibaranov/Documents/MONO/libc_test/libc_test/bin/Debug/libc.so'.
Mono: DllImport error loading library     '/home/ibaranov/Documents/MONO/libc_test/libc_test/bin/Debug/libc.so: cannot open shared     object file: No such file or directory'.
Mono: DllImport loading library:     '/home/ibaranov/Documents/MONO/libc_test/libc_test/bin/Debug/libc.so'.
Mono: DllImport error loading library         '/home/ibaranov/Documents/MONO/libc_test/libc_test/bin/Debug/libc.so: cannot open shared     object file: No such file or directory'.
Mono: DllImport loading location: 'libc.so'.
Mono: DllImport error loading library: '/usr/lib/x86_64-linux-gnu/libc.so: invalid     ELF header'.
Mono: DllImport loading location: 'libc.so'.
Mono: DllImport error loading library: '/usr/lib/x86_64-linux-gnu/libc.so: invalid ELF header'.
Mono: DllImport loading: 'libc.so'.
Mono: DllImport error loading library '/usr/lib/x86_64-linux-gnu/libc.so: invalid ELF header'.
Mono: DllImport unable to load library '/usr/lib/x86_64-linux-gnu/libc.so: invalid ELF header'.
Hello Linux!
Mono: DllImport attempting to load: 'libc.so'.
Mono: DllImport loading library: '/home/ibaranov/Documents/MONO/libc_test/libc_test/bin/Debug/libc.so'.
Mono: DllImport error loading library '/home/ibaranov/Documents/MONO/libc_test/libc_test/bin/Debug/libc.so: cannot open shared     object file: No such file or directory'.
Mono: DllImport loading library: '/home/ibaranov/Documents/MONO/libc_test/libc_test/bin/Debug/libc.so'.
Mono: DllImport error loading library '/home/ibaranov/Documents/MONO/libc_test/libc_test/bin/Debug/libc.so: cannot open shared object file: No such file or directory'.
Mono: DllImport loading location: 'libc.so'.
Mono: DllImport error loading library: '/usr/lib/x86_64-linux-gnu/libc.so: invalid ELF header'.
Mono: DllImport loading location: 'libc.so'.
Mono: DllImport error loading library: '/usr/lib/x86_64-linux-gnu/libc.so: invalid ELF header'.
Mono: DllImport loading: 'libc.so'.
Mono: DllImport error loading library '/usr/lib/x86_64-linux-gnu/libc.so: invalid ELF header'.
Mono: DllImport unable to load library '/usr/lib/x86_64-linux-gnu/libc.so: invalid ELF header'.

Unhandled Exception: System.DllNotFoundException: libc.so
  at (wrapper managed-to-native) libc_test.LibC:getpid ()
  at libc_test.MainClass.Main (System.String[] args) [0x00000] in <filename unknown>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.DllNotFoundException: libc.so
  at (wrapper managed-to-native) libc_test.LibC:getpid ()
  at libc_test.MainClass.Main (System.String[] args) [0x00000] in <filename unknown>:0 

I've been trying to resolve this problems for a long time, but nothing I have tried helps me. I use:

 dlopen("libc.so", RTLD_NOW);

Maybe I need libc.so compiled for x64?


回答1:


It should be

[DllImport("libc.so.6")]

or just

[DllImport("libc")]

"libc" is a alias from /etc/mono/config, On GNU/Linux system: ... <dllmap dll="libc" target="libc.so.6" os="!windows"\> ...




回答2:


to resolve this in x64 bit ubuntu 12

cd /lib/x86_64-linux-gnu
ln -s libc.so.6 libc.so



回答3:


To resolve on Raspbian on a Raspberry Pi with mono 4.0.2 compiled from a tarball, I did this

sudo cp /lib/arm-linux-gnueabihf/libc.so.6 /lib/libc

/lib being one of the default search folders



来源:https://stackoverflow.com/questions/14359981/mono-and-unmanaged-code-in-ubuntu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!