[fpc-pascal]How to use Linux libraries?

Iván Montes senbei at terra.es
Sat Jul 26 10:18:37 CEST 2003

----- Original Message ----- 
From: "Rainer Hantsch" <rainer at hantsch.co.at>
To: <fpc-pascal at lists.freepascal.org>
Sent: Saturday, July 26, 2003 9:06 AM
Subject: Re: [fpc-pascal]How to use Linux libraries?

> The better solution will definitely be to convert data inside my
> because it loads once and then can convert continuously picture by picture
> plan to use a WEB-CAM for something like a simple motion detection, i.e.,
so I
> will have to convert permanently, not only once at program start).

if you want to capture the data from a web-cam the best would be to directly
collect the data from it, it might be in a yuv color space which would be a
pain in the ass to convert.
You could use v4l perhaps? I don't know how to do it though. Haven't touch a
linux system in years :(

> I may be totally wrong, but as far as I understand, Linux' *.so files are
> same as Windoze' *.dll files in their meaning. So shouldn't it be possible
> use them in the same manner, by loading them and talking to them?

Yes Rainer, .dll and .so are the same. Generally speaking they are files
that contains code fragments (functions) much like an executable but that
can be loaded dynamically at runtime.
To use an .so library you need to load it in your program. Check the FPC
docs (http://www.freepascal.org/docs-html/prog/progch11.html#x197-19900011)
Under windows you can use LoadLibrary, GetProcAddress (to load functions)
and FreeLibrary, these functions are defined in the windows unit.
Under Linux you have to use dlopen, dlsym, dlclose which are in the shared
library 'dl'. As far as I know there is no interface to this library but
they are really easy to setup in your program:

//taken from the FPC OpenGL package by Sebastian Guenther (?)
  RTLD_LAZY         = $001;
  RTLD_NOW          = $002;

function dlopen(Name: PChar; Flags: LongInt) : Pointer;  extdecl; external
function dlsym(Lib: Pointer; Name: PChar) : Pointer;  extdecl; external
function dlclose(Lib: Pointer): LongInt;  extdecl; external 'dl';

function LoadLibrary(Name: PChar): THandle;
  Result := THandle(dlopen(Name, RTLD_LAZY));

function GetProcAddress(Lib: THandle; ProcName: PChar): Pointer;
  Result := dlsym(Pointer(Lib), ProcName);

function FreeLibrary(Lib: THandle): Boolean;
  if Lib = 0 then
    Result := False
    Result := dlClose(Pointer(Lib)) = 0;

//THandle should be defined in SysUtils but a 32bit integer should do the
job if you don't want to include the unit.

With those functions you can actually load dynamic (shared) libraries into
your program but you'll still need an interface to use that libraries
functions. In that interface you declare the data types and functions
without implementing them, sort of the 'interface' section of a pascal unit
or a C's .h file. It's worth noticing that there are two main calling
mechanism for functions STDCALL and CDECL, under linux I guess you'll find
mainly the CDECL type.

Check the zLib and OpenGL packages (among others) for FPC, they are
available on the FPC site. There you'll see how it works.

btw if you need a pascal interface to the libjpeg.so then you should
translate the libjpeg.h or whatever it's called to pascal. You can use the
h2pas util as a start.

as a final note, check thoroughly (sp?) the FPC docs, they are awesome and
they cover almost everything in a lot of detail. Check also the packages
sources and the contributed units, a lot of good tips can be learn from
there and they are usually very easy (short) to read sources.

I hope this long email has been of some help :)
ciao,  Ivan

More information about the fpc-pascal mailing list