How to explicitly load a binary module?
A binary module is a shared library that must have one entry point with a special name. For instance, if I have a binary module fred
then its initialisation function must be called luaopen_fred
. This code will explicitly load fred
given a full path:
local fn,err = package.loadlib('/home/sdonovan/lua/clibs/fred.so','luaopen_fred')
if not fn then print(err)
else
fn()
end
(For Windows, use an extension of .dll
)
Note that any function exported by a shared library can be called in this way, as long as you know the name. When writing extensions in C++, it is important to export the entry point as extern "C"
, otherwise the name will be mangled. On Windows, you have to ensure that at least this entry point is exported via __declspec(dllexport)
or using a .def file.