Load .so library dynamically from C

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Load .so library dynamically from C

Александр Машин-2
Dear all,

is there a C in Lua distribution function that does this?

static void luasandbox_load_lua_library( lua_State * L, const char *
path, const char * global ) {

     void * library;
     library = dlopen( path, RTLD_LAZY );
     if ( library ) {
         char * library_loader_name = emalloc( 9 + strlen( global ) );
         strcpy( library_loader_name, "luaopen_" );
         strcat( library_loader_name, global );
         void * library_loader;
         library_loader = dlsym( library, library_loader_name );
         lua_pushcfunction( L, library_loader );
         lua_call( L, 0, 1 );
         lua_setglobal( L, global );
     }
}


Alexander Mashin
Reply | Threaded
Open this post in threaded view
|

Re: Load .so library dynamically from C

Viacheslav Usov
On Mon, Sep 14, 2020 at 9:10 AM Александр Машин <[hidden email]> wrote:

> is there a C in Lua distribution function that does this?

I think your code is equivalent to the following Lua code:

_ENV[global] = package.loadlib(path, 'luaopen_' .. global)()

Then again, it is not clear why this should not just be:

_ENV[global] = require(global)

There are several ways to execute Lua code from a C host, for example,
luaL_dostring().

Cheers,
V.