Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Native P/Invoke with Mono on Linux: DllNotFound

I'm trying to load some native linux libraries using mono. I've run mono with the debug flag:

Mono: DllImport attempting to load: 'libavformat.57'.
Mono: DllImport error loading library '/home/filoe/Desktop/cscore/cscore/Samples/LinuxSample/bin/Debug/libavformat.57': '/home/filoe/Desktop/cscore/cscore/Samples/LinuxSample/bin/Debug/libavformat.57: cannot open shared object file: No such file or directory'.
Mono: DllImport error loading library '/home/filoe/Desktop/cscore/cscore/Samples/LinuxSample/bin/Debug/libavformat.57.so': 'libavcodec.so.57: cannot open shared object file: No such file or directory'.
Mono: DllImport error loading library '/usr/lib/libavformat.57': '/usr/lib/libavformat.57: cannot open shared object file: No such file or directory'.
Mono: DllImport error loading library '/usr/lib/libavformat.57.so': '/usr/lib/libavformat.57.so: cannot open shared object file: No such file or directory'.
Mono: DllImport error loading library 'libavformat.57': 'libavformat.57: cannot open shared object file: No such file or directory'.
Mono: DllImport error loading library 'libavformat.57.so': 'libavformat.57.so: cannot open shared object file: No such file or directory'.
Mono: DllImport error loading library 'libavformat.57': 'libavformat.57: cannot open shared object file: No such file or directory'.
Mono: DllImport unable to load library 'libavformat.57: cannot open shared object file: No such file or directory'.
Mono: DllImport attempting to load: 'libavformat.57'.

There are lots of lookup positions but at least one of them SHOULD match. This is how my directory looks like:

filoe@ubuntu:~/Desktop/cscore/cscore/Samples/LinuxSample/bin/Debug$ dir
CSCore.Ffmpeg.dll     CSCore.Ffmpeg.dll.mdb  CSCore.Linux.dll.config  FFmpeg     libavformat.57  libswresample.2  LinuxSample.exe.mdb
CSCore.Ffmpeg.dll.config  CSCore.Linux.dll   CSCore.Linux.dll.mdb     libavcodec.57  libavutil.55    LinuxSample.exe  log.txt
filoe@ubuntu:~/Desktop/cscore/cscore/Samples/LinuxSample/bin/Debug$ 

As you can see, the libavformat.57 is there. So is mono telling me that it could not be found?

The following code demonstrates what is done:

Declaration of some DllImport methods:

[DllImport("avformat-57", EntryPoint = "av_register_all", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void av_register_all();
[DllImport("avcodec-57", EntryPoint = "avcodec_register_all", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
internal static extern void avcodec_register_all();

The project contains also a file with the name "{name of the output assembly}.config":

<configuration>
  <dllmap os="linux" dll="avcodec-57" target="libavcodec.57"/>
  <dllmap os="linux" dll="avformat-57" target="libavformat.57"/>
</configuration>

As you can see above, the mapping works fine. Mono takes "avformat-57" and translates it to "libavformat.57". Now mono searches for a library with the name "libavformat.57" or some related names like "libavformat.57.so". Mono searches within the directory of the executing assembly.

But, it does not manage to find the file it is looking for(according to the log posted above). So why?

Thanks!

Regards

like image 414
Florian Avatar asked Dec 11 '16 18:12

Florian


1 Answers

The key was to use command

ldd libavformat.57

With the following output:

linux-vdso.so.1 =>  (0x00007ffdf9bd6000)
libavcodec.so.57 => not found
libavutil.so.55 => not found
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f4a74652000)
libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f4a74439000)
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f4a7421b000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f4a73e56000)
/lib64/ld-linux-x86-64.so.2 (0x00007f4a74d73000)

So I've renamed it to the suggested names and tried it again with no success. The next try with

LD_LIBRARY_PATH=./ ldd libavformat.so.57

Was successful. I've adjusted the config file and now I'm able to start the application with

LD_LIBRARY_PATH=./ mono MyApp.exe
like image 147
Florian Avatar answered Oct 04 '22 10:10

Florian