Recent

Author Topic: how to use shared library in linux  (Read 8082 times)

ciammaruca

  • New Member
  • *
  • Posts: 20
how to use shared library in linux
« on: November 07, 2014, 01:41:50 pm »
please, if possible, a working example for a simple example, for linux.
I have this classic example:
http://tldp.org/HOWTO/Program-Library-HOWTO/more-examples.html#AEN280
but this wiki is not clear for me:
http://wiki.freepascal.org/Lazarus/FPC_Libraries#Loadlibrary_-_dynamically_loading_a_dynamic_library

So, how to call hello function in libhello.so.0.0?
Please, if possible, a complete and working example, not teorically speaking, but for freepascal really beginners .
The only starting point is the  libhello.so.0.0 already made.
thanks

Fred vS

  • Hero Member
  • *****
  • Posts: 3939
    • StrumPract is the musicians best friend
Re: how to use shared library in linux
« Reply #1 on: November 07, 2014, 03:11:05 pm »
Hello.

Here how to dynamically load the library.

Code: [Select]
uses
...,
 DynLibs.pas,...
...
 var
mylib_handle :TLibHandle=dynlibs.NilHandle;

and , when you need your lib (could be at Form.OnCreate)

Code: [Select]
mylib_handle := LoadLibrary('mylib.so');

// according the lib is in executable folder, otherwise write the full path.

When you close your app (or if you do not need the lib)

Code: [Select]
UnloadLibrary(mylib_handle);

« Last Edit: November 07, 2014, 03:23:15 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Fred vS

  • Hero Member
  • *****
  • Posts: 3939
    • StrumPract is the musicians best friend
Re: how to use shared library in linux
« Reply #2 on: November 07, 2014, 03:22:34 pm »
... And for the hello function... =>

Code: [Select]

uses
...,
 DynLibs.pas,...
...
var
hello: function():String ; cdecl;
...
implementation
...
Pointer(hello):=DynLibs.GetProcedureAddress(mylib_handle,PChar('hello'));
...

And when you want to call hello function =>

Code: [Select]
...
 mystring := hello;
...

« Last Edit: November 07, 2014, 03:29:04 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

ciammaruca

  • New Member
  • *
  • Posts: 20
Re: how to use shared library in linux
« Reply #3 on: November 07, 2014, 03:32:41 pm »
ok,
so, i have this libhello.c compiled in libhello.so.0.0:
#include <stdio.h>
int hello(int a,int b) {
a=a+b;
return a;
}

and this is freepascal:
var
  Form1: TForm1;
  mylib_handle :TLibHandle=dynlibs.NilHandle;
  FuncResult:integer;
implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
 Mylib_handle := LoadLibrary('/home/rob/Copy/linux/programmazione-C/libreria/libhello.so.0.0');
 FuncResult:=hello(3,5);
 form1.edit1.text:=inttostr(FuncResult);

end.               

but: Error: Identifier not found "hello"

Fred vS

  • Hero Member
  • *****
  • Posts: 3939
    • StrumPract is the musicians best friend
Re: how to use shared library in linux
« Reply #4 on: November 07, 2014, 06:46:38 pm »
Hello

Code: [Select]
var
  Form1: TForm1;
  mylib_handle :TLibHandle=dynlibs.NilHandle;
  hello: function(a : integer; b : integer):integer ; cdecl;

implementation
...
procedure TForm1.FormActivate(Sender: TObject);
begin
 mylib_handle := LoadLibrary('/home/rob/Copy/linux/programmazione-C/libreria/libhello.so.0.0');
Pointer(hello):=DynLibs.GetProcedureAddress(mylib_handle,PChar('hello'));
 edit1.text:=inttostr(hello(3,5));
end;
...
end.               
« Last Edit: November 07, 2014, 06:57:17 pm by Fred vS »
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

ciammaruca

  • New Member
  • *
  • Posts: 20
Re: how to use shared library in linux
« Reply #5 on: November 07, 2014, 10:05:45 pm »
ola,
muchas gracias, ahora el codigo se va marchando derecho por derecho!
Now I'm going to bookmark this working example.
One more question, please:
what does it mean this line:
Pointer(hello):=DynLibs.GetProcedureAddress(mylib_handle,PChar('hello'));
Exactly, what does it happen to hello at: Pointer(hello)?
Because i left hello as an integer (hello: function(a : integer; b : integer):integer ; cdecl;)
So, in the last line (edit1.text:=inttostr(hello(3,5));) hello is used as integer or pointer? Or is it at the same time the integer with the Address as sender, and an integer as receiver?
thanks again

Fred vS

  • Hero Member
  • *****
  • Posts: 3939
    • StrumPract is the musicians best friend
Re: how to use shared library in linux
« Reply #6 on: November 08, 2014, 01:13:45 pm »
Quote
So, in the last line (edit1.text:=inttostr(hello(3,5));) hello is used as integer or pointer? Or is it at the same time the integer with the Address as sender, and an integer as receiver?

Your "hello" is defined as a variable and that variable is a function.

So pointer(hello) point to he function hello.

Bu maybe i did not understood your question.

Fre;D
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

 

TinyPortal © 2005-2018