[fpc-pascal] Calling a C library

Alain Michaud Alain.Michaud at nrc-cnrc.gc.ca
Tue Aug 9 17:52:38 CEST 2005


Hi,

  here is how I do a dynamic load to a library: (in Linux)

step 1 -  from the " .h" file prepare an interface section:

type
  ...
TPibcnt  = ^word;
Tibclr   = function(Device_Handle:smallint):smallint;cdecl;
Tibconfig =
function(Device_Handle,option,setting:smallint):smallint;cdecl;
Tibdev   =
function(board_index,pad,sad,timeout,send_eoi,eoi:smallint):smallint;cdecl;
...
var
  Pibcnt    : TPibcnt;
  ibclr     : Tibclr;
  ibconfig  : Tibconfig;
  ibdev     : Tibdev;
  ibeos     : Tibeos;
  ibfind    : Tibfind;
  iblines   : Tiblines;
  ibloc     : Tibloc;
  ibonl     : Tibonl;
  ibrd      : Tibrd;
  ibrda     : Tibrda;
... 

step 2 -  Load the module

HandleLIBGPIB := dlopen(GPIBLIBRARYNAME,RTLD_NOW);
if (HandleLIBGPIB = nil) then  ... //  NO Module !!!!

step 3 -  Get the procedures one by one:  (only the procedures that you
will use are necessary)


Pibcnt :=  TPibcnt(dlsym(HandleLIBGPIB,'ibcnt'));
if Pibcnt = nil then exit;

ibclr :=  Tibclr(dlsym(HandleLIBGPIB,'ibclr'));
if @ibclr = nil then exit;

ibconfig :=  Tibconfig(dlsym(HandleLIBGPIB,'ibconfig'));
if @ibconfig = nil then exit;

This is not as the ultimate code, but it works! The advantage of
"dynamic" loading as oposed to "compile time" loading is that you can
test the loading of the routines one by one. It is not easy to
understand the  " .h" file sometimes.

Good luck

Alain Michaud




On Tue, 2005-08-09 at 08:32 -0400, Paul Aviles wrote:
> How do you call a C library or .h file from FPC in linux? Any samples?
> 
> Thanks
> 
> Paul
> 
> 
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal





More information about the fpc-pascal mailing list