Re: [fpc-pascal]How to use Linux libraries?

2003-07-26 Thread Rainer Hantsch
Hi, Micha, and thanks for your detailled answer.

In fact, you are right. I do not force JPEG. Though, it is one of the most
common file formats.
I also thought about using an external program to convert any type of image
into one easy to read format and then reading this one in. This 'quick and
dirty method' may be ok as a workaround, but is not very optimal for permanent
usage, because I have to load&execute i.e. Imagemagick's 'convert' for every
particular picture. This is slow and causing a relatively high activity on the
harddisk, therefore I discontinued this thought very soon.

The better solution will definitely be to convert data inside my application,
because it loads once and then can convert continuously picture by picture (I
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).

What I am not understanding is:
---
Most programs I can see in SuSE Linux use shared object libraries. These
libraries are ready to use modules, can be used within multiple programs, and
save me from re-inventing all this basic stuff again and again. There already
was somebody who wrote these libraries and solved all problems with time, so
why should I decide to do the same bloody way? (I also use the MySQL library
without starting to analyze and re-code the whole communication protocol!)

What (in addition) is making me think is:
I have a friend who writes (with FPC) his own graphical applications for
Windoze - with complete GUI support. He wrote once a library to simplify the
API of Windoze, and now he can use Windoze in a procedural way. Looks
relatively easy. He simply uses existing Windoze *.dll files to do all this
basic stuff and also does not have to worry about how to read in a JPG in
detail. The *.dll's do this job, and he simply has to call them the right way.

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



| Oh, one more thing - Pasjpeg unit uses TStream classes and you said you
| dont like Classes. But, really, they are so simple - I guarantee that
| you will be glad when you learn how to use them and when you see what
| TStream and it's descendants can do for you.

Classes are possibly fine, otherwise they would not be such common. But as far
as I understand, you cannot mix Class Style programming and OOP programming
in one program (would also make life not easier, even when this would work).
This means that I would have to re-write a lot of code only to be able to use
it longer. --> Makes no real sense to me.


mfg

  Ing. Rainer Hantsch


___
fpc-pascal maillist  -  [EMAIL PROTECTED]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal]How to use Linux libraries?

2003-07-26 Thread Iván Montes
- Original Message - 
From: "Rainer Hantsch" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
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
application,
> because it loads once and then can convert continuously picture by picture
(I
> 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
the
> same as Windoze' *.dll files in their meaning. So shouldn't it be possible
to
> 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 (?)
const
  RTLD_LAZY = $001;
  RTLD_NOW  = $002;
  RTLD_BINDING_MASK = $003;

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


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

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

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

//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


___
fpc-pascal maillist  -  [EMAIL PROTECTED]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal]How to use Linux libraries?

2003-07-26 Thread Marco van de Voort
> to
> > 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:

Don't! Use unit dynlibs. (there are some examples in docs/dynlibex)

Dynlibs is basically a very small wrapper that encapsulates this. However
it will avoid quite a few platform dependant ifdef's.
 
> //taken from the FPC OpenGL package by Sebastian Guenther (?)
> const
>   RTLD_LAZY = $001;
>   RTLD_NOW  = $002;
>   RTLD_BINDING_MASK = $003;
> 
> function dlopen(Name: PChar; Flags: LongInt) : Pointer;  extdecl; external
> 'dl';
> function dlsym(Lib: Pointer; Name: PChar) : Pointer;  extdecl; external
> 'dl';
> function dlclose(Lib: Pointer): LongInt;  extdecl; external 'dl'; 

Since e.g. these are exactly the same on BSD, just in lib 'c'.

etc etc.


___
fpc-pascal maillist  -  [EMAIL PROTECTED]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal]How to use Linux libraries?

2003-07-26 Thread Michael . VanCanneyt


On Sat, 26 Jul 2003, Rainer Hantsch wrote:

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

You can. Using them statically (i.e. the system loads the needed library)
is transparant on all systems. See the manual, chapter I advised you. It
describes exactly what you need.

To load libraries dynamically, you can use the dynlibs unit.
It contains cross-platform handling of shared libraries.


> | Oh, one more thing - Pasjpeg unit uses TStream classes and you said you
> | dont like Classes. But, really, they are so simple - I guarantee that
> | you will be glad when you learn how to use them and when you see what
> | TStream and it's descendants can do for you.
>
> Classes are possibly fine, otherwise they would not be such common. But as far
> as I understand, you cannot mix Class Style programming and OOP programming
> in one program (would also make life not easier, even when this would work).

It _IS_ possible to do so. Only you cannot do the following:

  TA = Object
   ...
  end;

and then
  TB = Class(TA)
  ...
  end;

i.e. you cannot mix class/object descendents. But you can use them together
in 1 program, without problem.

Michael.


___
fpc-pascal maillist  -  [EMAIL PROTECTED]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal