[Mono-list] How to create C++ library and call it's functions from C# program?

Vadim B. Guzev vguzev@yandex.ru
Thu, 11 Mar 2004 23:25:18 +0300


Hello, mono-list@lists.ximian.com!


I'm trying to call some simple C++ functions from C# program.
Here's the sources I used:

8<------------------------------------------------------------
[vadim@server SCSI tests]$ cat FromCSharpToC.cs
using System;
using System.Runtime.InteropServices;

public class FromCSharpToC {
 [DllImport("libmylib.so", EntryPoint="print")]
 static extern void print (string src);

 public static void Main( string[] args ) {
  print("Hello world!");
 }
}

8<------------------------------------------------------------

8<------------------------------------------------------------
[vadim@server SCSI tests]$ cat mylib.cpp
#include <iostream.h>

static void print( const char* line ) {
 cout << line << endl;
}

8<------------------------------------------------------------

And here is a Makefile I used:
8<------------------------------------------------------------
[vadim@server SCSI tests]$ cat Makefile
all: o so exe


o:
        gcc -fpic -c mylib.cpp

so:
        gcc -shared -o libmylib.so mylib.o

exe:
        mcs FromCSharpToC.cs -o fromcsharptoc.exe

8<------------------------------------------------------------

However, when I try to launch fromcsharptoc.exe it gives me the following:

8<------------------------------------------------------------
[vadim@server SCSI tests]$ make
gcc -fpic -c mylib.cpp
gcc -shared -o libmylib.so mylib.o
mcs FromCSharpToC.cs -o fromcsharptoc.exe
Compilation succeeded
[vadim@server SCSI tests]$ mono fromcsharptoc.exe

** (fromcsharptoc.exe:26607): WARNING **: Failed to load library
./libmylib.so (libmylib.so): ./libmylib.so: undefined symbol:
__gxx_personality_v0

** (fromcsharptoc.exe:26607): WARNING **: Failed to load library
./libmylib.so (libmylib.so): ./libmylib.so: undefined symbol:
__gxx_personality_v0

Unhandled Exception: System.MissingMethodException: A missing method
exception has occurred.
in <0x00042> (wrapper managed-to-native) .FromCSharpToC:print (string)
in <0x0000c> .FromCSharpToC:Main (string[])

8<------------------------------------------------------------

Where's a mistake? Maybe I compile C++ source in a wrong way? (I'm a newbie
in C++-programming).
Is there some good tutorials on this theme? (I couldn't find anything :( )


Best regards,
Vadim B. Guzev