Install Free Pascal:
Compile: (Ignore any warning about missing a -T switch. It's supposedly a linker bug.)sudo apt-get install fp-compiler
Run:fpc main.pp -omainp
./mainp
Uses dynlibs; {LoadLibrary, GetProcAddress, FreeLibrary}
Var
Lib : THandle;
Add : Function(a, b : Integer) : Integer; Stdcall;
PrintMe : Procedure(a : PChar); Stdcall;
Begin
{ Load the library from disk }
Lib := LoadLibrary('./libhello.so');
{ Check for success }
If Lib >= 32 Then
Begin
{ Locate add function and, if found, call it }
Pointer(Add) := GetProcAddress(Lib, 'add');
If Add = Nil Then
WriteLn('Unable to find add.')
Else
WriteLn('The result is: ', Add(5, 6));
{ Locate the printme procedure and, if found, call it }
Pointer(PrintMe) := GetProcAddress(Lib, 'printme');
If PrintMe = Nil Then
WriteLn('Unable to find printme.')
Else
PrintMe('Hello from Free Pascal!');
{ Clean up }
FreeLibrary(Lib);
End
Else
WriteLn('Failed to load library: ./libhello.so');
End.
Output:
The result is: 11 Hello from library: Hello from Free Pascal!