BrowserUk has asked for the wisdom of the Perl Monks concerning the following question:
Can one XS library easily make use of the functions within another XS library? If so, how? Are there any examples of this?
I know of two ways to do it.
First, you can go through the Perl interface using the callback API from perlcall. That would definitely not qualify as "easy", though.
Alternatively, you can get the source library to export its C functions somehow. There are numerous ways to accomplish this, some of which are discussed in Dynaloader/XS: sharing C symbols across shared objects. Cross-platform exporting is difficult. I ultimately adopted the technique from Time::HiRes described by salva.
Here's code from Snowball.xs which makes 4 symbols from the Snowball stemming library available (sb_stemmer_list, etc):
That works for a small list of functions. It would be cumbersome and impractical for a large export list.
If I understand that right--and I'm not certain I am--you are importing the Perl callable functions in one XS module, into the namespace of another XS module?
If so, that's not quite what I'm after--though I understand that my question was unclear.
My thought--albeit perhaps not fully thought through--is to have an XS module that is callable/usable from Perl in the normal way. But to also allow other XS modules to make use of the internal C functions (that sit behind the Perl-callable XS interface), directly at runtime. That is, without #includeing the sources, or linking against the objects or a lib. There would be two or three entrypoints to import at most.
The notion was to remove the compile-time dependancy upon the headers or a lib for potential XS users of the module. They could then decide at runtime whether it is available and use it. Or fallback to other mechanisms. Ie. If the module is installed locally, load it and "obtain the required entrypoints".
Hm. I know I can do this on Windows. LoadModule()/GetProcAddr() are all I need. But I would need a portable solution, and I don't know enough about other platforms to know if there are equivalents.
What about creating a nice set of C wrapper functions for the OS dependant LoadLibrary/GetProcAddr/FreeLibrary calls? Makefile.PL / Build.PL could decide which one to use, or panic if the OS is unknown / not implemented.
BTW: Linux seems to use dladdr, dlclose, dlerror, dlopen, dlsym, dlvsym, the first four are POSIX.1-2001, the other two are GNU extensions, according to the man page. So, if you use the Win32 API for Windows as a special case, and simply try to use dlopen and friends on other platforms, you could get the job done for most Unix platforms. Other platforms should fail and complain loudly during make.
Alexander.
If I understand that right--and I'm not certain I am--you are importing the Perl callable functions in one XS module, into the namespace of another XS module?
He's saving the address of functions callable from C in the current Perl package.
Does this help? ExtUtils::Depends
No, not exactly. Actually, not even slightly. That requires that in order to make use of the module, that it be available at install/compile time. That pretty much belies the reasoning for having dynamic linking.
What I'm pursuing is a cross-platform way of expressing these two operations:
No recent polls found