[fpc-pascal] Interfacing to libraries written in C
Alain Michaud
Alain.Michaud at nrc-cnrc.gc.ca
Mon Mar 14 18:23:13 CET 2005
Hi,
I see many messages about linking libraries that were written in C
language. I do it at run time. This message is for those that are not so
familiar with linux system programming. Here is an exampe:
Supose that I want to use the the function "ibdev" in the module
"libgpib.so". From the file "libgpib.h" I could learn more about
procedure parameters. Then I found the coresponding types in pascal and
wrote my own pascal program:
...
uses libc;
...
type
Tibdev =
function(board_index,pad,sad,timeout,send_eoi,eoi:smallint):smallint;cdecl;
...
var
HandleLIBGPIB : pointer;
ibdev : Tibdev;
...
const
GPIBLIBRARYNAME : pchar = '/usr/local/lib/libgpib.so';
...
begin
...
HandleLIBGPIB := dlopen(GPIBLIBRARYNAME,RTLD_NOW);
if (HandleLIBGPIB = nil) then
begin
showmessage('Library file not found: '+GPIBLIBRARYNAME);
exit;
end;
...
ibdev := Tibdev(dlsym(HandleLIBGPIB,'ibdev'));
if @ibdev = nil then exit;
...
Then the procedure "ibdev" can be used normally. I think that:
1 - It is easy to check and take action if the library is not loaded,
properly. My program is more robust.
2 - I find that it is not easy to understand the "C" types sometimes.
This "dynamic" techniqe can be used to test and debug all the procedures
one by one. Also you can declare only the procedures that you will be
using, and not bother about the ones that you do not understand.
I hope that this can be usefull. Thank you
Alain Michaud
amiso
rubidium
More information about the fpc-pascal
mailing list