@nanobit @Thaddy thanks, I'll check libffi presently.
The gist of the program I've written is that it takes a .inc file which is pretty much exactly what you'd use for static linkage, except that the last bit of each procedure/function declaration is replaced by a macro name. It generates two unit source files:
* A static unit which includes the .inc file expanding the macro.
* A dynamic unit which doesn't expand the macro, but declares an object with methods each of which is a thin wrapper around (a pointer to) an imported function (@nanobit, this is probably equivalent to what you've suggested). This also "automagically" generates all procedure/function types, fields of those types to hold function pointers, the implementation of each thin wrapper and so on, and as icing on the cake keeps comments in a format compatible with Lazarus hinting.
There's also provision for a file containing locally-written replacements for C/C++ macros.
The nice thing is that by naming the static unit and the dynamic object the same, the code at which imported functions are used is completely oblivious as to whether static or dynamic linkage is currently being used (except that dynamic vararg functions have to be imported explicitly, since wrappers can't be written).
The overall layout of the .inc file is something I hacked together quite some years ago, based on a Delphi interface somebody gave me which might have originated at NAG. But I also find that with care one can use the same file when *creating* a library, hence one can have common definitions (plus magic number protection etc.) when defining a frontend/backend architecture.
I've tacked on an example: the .inc files are created manually but the others by the program. The advantage doesn't really show for something this small, but by the time one has a dozen or so functions (e.g. for even a minimal ALSA interface
https://github.com/MarkMLl/asound ) the work involved if it's done by hand is substantial. I'm not sure that the program is ready for "general release" yet, or who'd be interested, or whether I want to commit myself to unending maintenance as is apparently necessary for e.g. fpcupdeluxe.
MarkMLl