Mainstream. Many times, some key applications (such as large-scale cartesian product operation, which needs to call platform hardware functions) depend on C.
Mono seems to be at a loss at this time.
Of course, you can use the DLLImport feature to complete the call to the local library. So). Now let's talk about how to generate your own local library and call it in Mono.
Write a simple function in C first, which is only used to add two shaping parameters and return the result.
//The file name is sum.c
# include & ltstdio.h & gt
int sum(int a,int b)
{
Return a+b;
}
All right. Now we need to generate a target local library named libsum.so, and compile it with GCC compiler to generate the desired results.
Gcc-wall-fpic-O2-c-o libsum。 Oh, sum. C// generation. o
Gcc-shared-wl,-soname,libsum。 so-o libsum。 Solisum.o//Only in this step can the library be shared. This generates the file.
At this point, Libsum.so has been generated. Then you can use it in mono.
Use the system;
Use the system. Runtime . InteropServices
Public class UnsafeDemo
{
[dllimport ("libsum.so ",EntryPoint =" sum ")]// Be sure to specify entry point.
Static extern int sum(int a, int b);
Unsafe static void Main ()
{
int x = sum(23,45);
Console. WriteLine("x: {0} ",x);
}
}
Then you can compile our program:
$ mcs-UnsafeDemo.cs
$ TERM Mono UnsafeDemo.exe
If you are prompted that libsum.so cannot be found when compiling the program, you can try to put libsum.so in the same directory as UnsafeDemo.exe.
If you don't succeed, you can also refer to my article. Good luck!
Zhang Jianying/archive/2006/09/04/1177049.aspx
Finally, when writing C, remember that C uses the return value to judge whether there is an error. Mono uses exceptions.
//C code snippet
Int debugging (character)
{
Printf ("output: %s/n", s);
return- 1;
}
//Mono code segment
[DllImport ("libsum.so ",EntryPoint="debug")]
Static extern int debug (string sb);
Unsafe static void Main ()
{
int x = debug(" hello ");
If (x)
{
//Success
}
other
{
//failed
Throw newyouexception ();
}
}
reprint