[Mono-devel-list] How to use shared library with C# on linux
Gonzalo Paniagua Javier
gonzalo at ximian.com
Wed Nov 19 14:45:25 EST 2003
El mié, 19-11-2003 a las 18:23, Daniel Schalk escribió:
> Hi,
>
> I'm trying to code a C# app, which calls a C-function out of a shared
> library (e.g. testlib.so).
> In the testlib.cc i placed the following code:
>
> #include <stdio.h>
>
> void fktTest(void)
> {
> printf("called fktTest");
> }
>
> I compiled it with: gcc -c -fpic testlib.cc
> then: gcc -shared -fpic -o testlib.so testlib.o
>
> Now I created LibTest.cs:
>
> using System;
> using System.Runtime.InteropServices;
>
> namespace LibTest
> {
> class LibraryTest
> {
> [DllImport("testlib", EntryPoint="fktTest")]
> public static extern void fktTest();
>
> static void Main(string[] args)
> {
> fktTest();
> }
> }
> }
>
> I compiled it with : mcs LibTest.cs
> So far, there were no errors. Because I'm novice on linux (and mono :-)),
> was it right, what I did so far? Are there any faults within my code/doing?
No, except for the .cc already pointed out.
> Further questions:
> How do I have to go on to get running my application?
> Where to place the .so file?
>
> Because if I run the app with the .so in the same directory, I get error
> "unhandeled exception: System.MissingMethodException ...").
> What is wrong? After one day of "googling", I didn't find a solution.
I generated the .so as you did and used your test program. It worked
right away.
IF it still doesn't work for you, try this command when linking:
gcc -shared -fpic -o libtestlib.so testlib.o
and that will probably work.
-Gonzalo
More information about the Mono-devel-list
mailing list