[fpc-devel] Dynamically Loading Libraries

Ivo Steinmann ivo_steinmann at gmx.net
Mon Nov 2 22:20:46 CET 2009


Vincent Snijders schrieb:
> Ivo Steinmann schreef:
>> Most libraries are translated by a tool like h2pas from the original
>> headers and then rehashed manually. The best solution would be, if
>> there's a tool that generates the var procedures directly from all
>> external procedures.
>
> What do you exactly mean? A tool which takes for example:
> http://svn.freepascal.org/trunk/packages/libpng/src/png.pp
> and outputs a png_dyn.pp.
>
>>
>> But else it's much simpler to maintain a construct as the one above than
>> allways starting from the orignal h file or updating allways both
>> declarations. I know what i'm talking about, since I translated about 15
>> headers in /fpc/packages ^^
>>
>> But of course you are right, it's not the "nice" solution ;) The best
>> solution would be to have got some support from the compiler itself ;)
>>
>
> What kind of help?
>
> Vincent
> _______________________________________________
> fpc-devel maillist  -  fpc-devel at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-devel
>

well, there are a lot of possibilities, but that's one:

unit foobar;

{$DYNLOADING ON}
function foobar_dosomething(a:cint; b:cdouble): cint; extdecl;external
foobarlib;


Now when compiler flag {$DYNLOADING ON} is set, the compiler don't do
smart/static linking.

Internally it creates an array of all USED!! symbols (similar to the one
I recommended) and also generates functions like

InitializeFoobar;
TryInitializeFoobar;  // silent
FinalizeFoobar;

Then the user have to call this functions before he can use one of the
external symbols. If one of the used symbols is not found in the
library, we get an error similar to when do smart linking, but at runtime.

This way we could use the same headers as dynamic and static, just by
switching {$DYNLOADING ON} or OFF

This mechanism would be also interesting for packages in lazarus.

-Ivo



More information about the fpc-devel mailing list