[Mono-devel-list] DllImport with multiple shared libraries

Heribert.Schuetz.extern at HVB.de Heribert.Schuetz.extern at HVB.de
Thu Dec 23 14:51:22 EST 2004


Hi,

I'm trying to call into native code from C# within Mono (on Linux). It works
fine as long as the native function is completely implemented in a single
shared library. If, however, this shared library requires another shared
library, I don't know how I should tell Mono about this other library. The
DllImport attribute doesn't seem to provide a place for this. And if I only
mention the first library, I get a "relocation error" (with an undefined
symbol from the second shared library) when trying to invoke my native
function.

A workaround was to import a dummy function from the second shared library
and to call it from the C# code before calling the actual entry point of the
first shared library. Is there a cleaner way to achieve this?

A related issue: In a simple example, I can also implement my shared library
in C++ and it works. But as soon as I add "#include <iostream>" to my file
(even without actually using any iostream features in my code), I get a
DllNotFoundException. How can this be avoided? (I first thought that this is
the same problem as above because Mono does not know about libstdc++.so,
which might be needed to resolve some symbols from the iostream header.  But
the reported problems are different.)

Could anybody give me some advice here?

I am using the binary Mono distribution 1.0.5 for Red Hat 9 (albeit on
Red-Hat Advanced Server 3.0) and gcc 3.2.3.

Thanks in advance,
Heribert.

PS: Here is some simple example code:

native.cs:
--------------------------------------
using System;
using System.Runtime.InteropServices;

class Test {

  [DllImport("native2")]
  public extern static int multiply(int x, int y);

  [DllImport("native")]
  public extern static int square(int x);

  public static void Main() {
    // Omitting the following line leads to a relocation error:
    Console.WriteLine("{0} * {1} = {2};", 5, 9, multiply(5, 9));

    Console.WriteLine("square({0}) = {1};", 6, square(6));
  }
}
--------------------------------------

native.cpp:
--------------------------------------
#include <vector>

// Enabling the following line leads to a DllNotFoundException:
// #include <iostream>

extern "C" {
    int multiply(int x, int y);
    int square(int x);
}

int square(int x) {
    std::vector<int> v(2);
    v[0] = x;
    v[1] = x;
    return multiply(v[0], v[1]);
}
--------------------------------------

native2.c:
--------------------------------------
int multiply(int x, int y) {
    return x * y;
}
--------------------------------------

Makefile:
--------------------------------------
all: run

native.o: native.cpp
	g++ -c -o $@ -fPIC $<

native.so: native.o
	ld -shared -o $@ $^

native2.o: native2.c
	gcc -c -o $@ -fPIC $<

native2.so: native2.o
	ld -shared -o $@ $^

native.exe: native.cs
	mcs $<

run: native.exe native.so native2.so
	LD_LIBRARY_PATH=.:/usr/lib/gcc-lib/i386-redhat-linux/3.2.3 mono $<

clean:
	rm -f *.o *.so *.exe *~
--------------------------------------



More information about the Mono-devel-list mailing list