lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

Hi lhf, rtt, and everybody,

>>So, yes we need a good way to add third party libraries to an existing Lua
>>executable without having to recompile it.
>I think loadlib can help here:

But loadlib does not work with Lua 4.0... I tried to patch it a little
to make it work with 4.0 but couldn't (it would be the first time I
programmed a Lua extension in C, BTW), and I ended up reimplementing a
much simpler "loadlib" function, whose bulk is just this

#include <lualib.h>
#include <dlfcn.h>

static int loadlib(lua_State *L) {
  void *libptr;
  lua_CFunction f;
  libptr=dlopen(lua_tostring(L, 1), RTLD_LAZY);
  if (libptr==0)
    luaL_argerror(L, 1, "dlopen cannot open shared library");
  else {
    f=dlsym(libptr, lua_tostring(L, 2));
    if (f==0)
      luaL_argerror(L, 2, "dlsym cannot find init function");
  return 0;

LUALIB_API void lua_loadlib_open(lua_State *L) {
  lua_register(L, "loadlib", loadlib);

For how to install it and test it, how to download the Debian package,
how to recompile it include this function in the library, etc, etc,


Cheers, and please provide feedback,
  Eduardo Ochs