FFI library dependency howto ?

nicolas cellier ncellier at ifrance.com
Fri May 19 19:54:36 UTC 2006

Hi nice
(I answer myself in case it help anyone else)

Le Jeudi 18 Mai 2006 21:49, nicolas cellier a écrit :
> Hi all,
> I did not manage yet to understand how library dependency is handled in
> FFI, especially on linux.

gogle it and you will discover dlopen() in *nix, so man dlopen will help you.

> For example, i have liblapack.so depending on libblas.so, depending on
> libg2c, libm etc... Do they load automagically (sort of ldd machinery) ?

man dlopen again, but do also check in vm sources, you will discover the '.so' 
suffix and eventually the 'lib' prefix are not necessary.
Unless you specify an absolute directory, current directory from which squeak 
is launched is always scanned first.
If not found, then LD_LIBRARY_PATH, /etc/ld.so.cache, /lib and /usr/lib

If your library have dependents, the current directory search trick won't help 
anymore, unless it is in your LD_LIBRARY_PATH. Otherwise, your library need 
to be officially cached  (man ldconfig might help you also).

> I also need to overload some function (by default, errors are aborting the
> program in blas xerbla function), so load order is significant for me.

man dlopen says there is a RTLD_GLOBAL flag for that purpose, unfortunately it 
is not used by the vm.
If you do not want to rebuild the library with your version of xerbla, then 
you'll have to hack the vm, or maybe simply call dlopen through FFI with this 
flag set.

Note that you cannot write RTLD_GLOBAL from within Squeak, you must use 
underlying potentially platform dependant number... unless you use a C 
precompiler trick thru an external process call (more than heavy)...
Hey, don't blame squeak for C not being interpreted...

> Naturally, following question is how do i manage Multiplatform
> compatibility? In my case, i know external functions have the same
> prototype, i only have to hook to the right libraries...

This time, you have to browse the FFIPlugin that is written in Smalltalk.
so as to learn how and when module is hooked when unspecified. And if it's not 
in your shiny 3.9 image, browse SqueakMap, Monticello or download a current 
3.8 full image...

From there, you will find a way to set module name according to platform 
(SmalltalkImage browse) in the class side of your library at image startup 
(senders of #startup:).
Maybe you can let your user set their Preferences. If you cannot fix it by 
yourself, maybe they are more clever than you, give them a chance.

> Other questions pending: can i link a function returning a structure by
> value (the case of double complex functions...) ? Or am i forced to write a
> wrapper function ?

Give it a try, and tell us.

> Any help, link, or better any SqueakMap/SqueakSource available application
> providing good FFI examples appreciated, thanks

Except source code that is always the ultimate documentation, I din't find 
much, but hey, we must have the same bad eyes.

> Nicolas

More information about the Squeak-dev mailing list