Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2005-08

Calling a C library

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Alain Michaud
to Paul Aviles

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

Paul Aviles wrote 10 lines
How do you call a C library or .h file from FPC in linux? Any samples? Thanks Paul _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email