[Mono-list] C/C++ issue

thor71 at gmail.com thor71 at gmail.com
Fri Nov 23 09:46:04 EST 2007


Hi!

I'm trying to access a C++ (extern C) function from a C# program under mono.
It fails for me. With a plain C function it works fine.

Here is some sample code:

HelloSample.cs
<-- snip  -->
using System.Runtime.InteropServices;

class HelloSample {
   [DllImport("libhello_c")]
   static extern void print_hello_world_c();

   [DllImport("libhello_cpp")]
   static extern void print_hello_world_cpp();

   public static void Main() {
      print_hello_world_c();
      print_hello_world_cpp();
   }
}
<-- snip -->


hello_c.c
<-- snip -->
#include <stdio.h>

void print_hello_world_c() {
   printf("C Hello world... \n");
}
<-- snip -->


hello_cpp.cpp
<-- snip -->
#include <stdio.h>

extern "C"
{
   void print_hello_world_cpp() {
      printf("C++ Hello world... \n");
   }
}
<-- snip -->


Makefile:
<-- snip -->
all: HelloSample.exe libhello_c.so libhello_cpp.so

HelloSample.exe: HelloSample.cs
    gmcs HelloSample.cs

libhello_cpp.so: hello_cpp.cpp
    gcc --shared -o libhello_cpp.so hello_cpp.cpp

libhello_c.so: hello_c.c
    gcc --shared -o libhello_c.so hello_c.c
<-- snip -->


Testrun:
<-- snip -->
knorrhane mono_test/mono_test4 $ ls
hello_c.c  hello_cpp.cpp  HelloSample.cs  HelloSample.exe  libhello_cpp.so
libhello_c.so  Makefile
knorrhane mono_test/mono_test4 $ mono HelloSample.exe
C Hello world...

Unhandled Exception: System.DllNotFoundException: libhello_cpp
  at (wrapper managed-to-native) HelloSample:print_hello_world_cpp ()
  at HelloSample.Main () [0x00000]
knorrhane mono_test/mono_test4 $
<-- snip -->

I'm running Fedora 7, gcc 4.1.2 and Mono 1.2.3.

Any idea what my problem is?

/jonas

-- 
Jonas Thor
direct/mobile: +46(0)18-610 22 62
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-list/attachments/20071123/ce44d5da/attachment.html 


More information about the Mono-list mailing list