Monday, February 14, 2005

P/Invoke fun

So on the weekend I was trying to work on the next step in my vision project (really must get around to putting up a web page about it) and struck a wee problem with P/Invoke and marshalling using Mono on linux.

Actually it was two wee problems.

I was getting a run time exception saying that Mono couldn't find the library I was trying to use via DllImport. I thought this was strange because the library was definitely in my ld.so.cache and I was pretty sure I had my C# code correct.

I decided to try a different approach and use an example I knew should work to test if it was in fact my code/c library combination that was causing it to fail, or if it was a problem with my Mono environment. So I tried the ncurses example from Mono: A Developer's Notebook (excellent book by the way) and what do you know? It didn't work :-(

Well, after a bunch of emails traded with some very helpful guys on the Mono-List I was getting close to working out the problem. After turning on logging and info using the MONO_INFO and MONO_LOG enviroment settings (see "man mono" for more details) I was able to see that Mono was infact finding the assembly but not loading it for some reason. Another guy suggested it might be something to do with gmodule. Well, I'd had some problems with a few other apps after I had changed to gcc 3.4.3 from gcc 3.3.4 recently so I switched back to gcc 3.3.4 recompiled gmodule and hey! it works.... kinda

So, now the ncurses example was working but I still couldn't use P/Invoke with my target C library (the opencv library). Another helpful soul had suggested that if I use ldd to examine libopencv.so I would see that there was no reference to libstdc++ which was causing my problem. This was indeed the case, and after hacking a few linker steps in the opencv makefiles I was happily creating CvImage objects and loading highgui windows from my C# app!


Ahhhh... I just love it when I learn something new, and this time round I learnt a whole lot!

0 Comments:

Post a Comment

<< Home