Fork me on GitHub

PInvoke

Gentoo Mono Handbook
.pc-files
General overview of .so files:
http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html

http://www.mono-project.com/docs/advanced/pinvoke/#linux-shared-library-search-path

Installation

https://wiki.gentoo.org/wiki/Clang

use /etc/portage/env file to enable the use of clang per-package.
create a new environment override file (/etc/portage/env/clang):
CC=clang
CXX=clang++
enable use of clang for packages using /etc/portage/package.env file
app-foo/bar clang
app-bar/baz clang

Packaging

libraries are installed with dolib in portage - https://devmanual.gentoo.org/function-reference/install-functions/
dolib.so
Install a library (shared object) file
newlib.so
Install a .so library file using the second argument as the name

Building automation (Autotools)

https://www.gnu.org/software/automake/manual/html_node/A-Shared-Library.html#A-Shared-Library

https://www.gnu.org/software/automake/manual/html_node/A-Library.html

http://mij.oltrelinux.com/devel/autoconf-automake/

according to the autoconf manual, autoconf will execute a configure.gnu script in the subdirectory if it exists.

Checking/debugging

LD_LIBRARY_PATH is a colon-separated list of directories
LD_LIBRARY_PATH=.:$LD_LIBRARY_PATH my_program

Calling

http://www.mono-project.com/docs/advanced/pinvoke/

In principle, all you need to do is create a DllImport function declaration for the existing code to invoke, and the runtime will handle the rest. For example:
[DllImport ("shared-objects.dll")]
private static extern int calc(int a, int b);
It is possible to redefine name with myassembly.dll.config file

<configuration>
  <dllmap dll="shared-objects.dll" target="libshared-objects.so.1" />
</configuration>

C ABI is assumed (extern "C"), under Unix platforms, System.Runtime.InteropServices.CallingConvention.Cdecl is the default.

The general rule of advice is this: never pass classes or structures containing members of reference type (classes) to unmanaged code. This is because unmanaged code can’t do anything safely with the unmanaged reference (pointer), and the CLI runtime doesn’t do a deep marshal” (marshal members of marshaled classes, and their members, ad infinitum).

The immediate net effect of this is that you can’t have array members in marshaled classes

For simple types, such as integers and floating-point numbers, marshaling is a bitwise-copy (Njlitting”), just as would be the case for unmanaged code. In some cases, marshaling can be avoided, such as when passing structures by reference to unmanaged code (a pointer to the structure is copied instead). It’s also possible to obtain more control over marshaling, through custom marshaling and manual marshaling.
Mono on all platforms currently uses UTF-8 encoding for all string marshaling operations.
StringBuilder gets special marshaling support from the runtime. Always call EnsureCapacity() before passing it into the native method
you MUST use the StructLayout attribute and specify a LayoutKind value of LayoutKind.Sequential or LayoutKind.Explicit
structures are LayoutKind.Sequential by default,

What do you do if you don’t want the runtime to free the memory? Don’t return a class. Instead, return an IntPtr (the moral equivalent of a C void* pointer), and then use the Marshal class methods to manipulate that pointer, such as Marshal.PtrToStructure, which works for both C# struct types and class types marked [StructLayout(LayoutKind.Sequential)].

Compiling (clang)

http://clang.llvm.org/docs/CommandGuide/clang.html
You can pass the soname the -Wl flags to clang:
clang -shared file.o -Wl,-soname,libfile.so.8 -o file.so
the -shared command line parameter - http://stackoverflow.com/questions/9170000/how-use-llvm-linker

Creating

.so library example
https://developer.apple.com/library/mac/documentation/DeveloperTools/Conceptual/DynamicLibraries/100-Articles/CreatingDynamicLibraries.html