简体   繁体   中英

mono and unmanaged code in ubuntu

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?

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"\\> ...

to resolve this in x64 bit ubuntu 12

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM