My lua script load .so library, how can I write the host program with Lua 5.2?

StackOverflow https://stackoverflow.com/questions/15893165

  •  02-04-2022
  •  | 
  •  

Domanda

I searched and tried for days. The problem is this:

I wrote a script which load a shared library locker.so, it runs well with lua interpretor, but I can not write out the correct host program.

My lua script load_so.lua is very simple:

locker = require("locker")
print(type(locker))
for k, v in pairs(locker) do
    print(k, v)
end

My host program is:

int main(int argc, const char *argv[])
{
    lua_State * L = luaL_newstate();
    luaL_openlibs(L);

    if (luaL_dofile(L, "load_so.lua") != 0) {
        fprintf(stderr, "luaL_dofile error: %s\n", lua_tostring(L, -1));
        lua_pop(L, 1);
    }
    lua_close(L);
    return 0;
}

When I run my host program, error print out:

luaL_dofile error: error loading module 'locker' from file './locker.so':
    ./locker.so: undefined symbol: lua_pushstring

And the locker.c:

static int elock_get(lua_State * L) {...}

static int elock_set(lua_State * L) {...}

static const struct luaL_Reg lockerlib[] = {
    {"get", elock_get},
    {"set", elock_set},
    {NULL, NULL}
};

int luaopen_locker(lua_State *L)
{
    //luaL_newlib(L, lockerlib);
    //lua_pushvalue(L, -1);
    //lua_setglobal(L, LOCKER_LIBNAME);
    //set_info(L);
    luaL_newlibtable(L, lockerlib);
    luaL_setfuncs(L, lockerlib, 0);
    return 1;
}

Most articles, books, questions shows how to do it in Lua 5.1, and yes, the program runs correctly in Lua 5.1. But how can I make it support Lua 5.2, and why?

P.S: I don't want to load the library in my C host program like luaL_requiref(L, "locker", luaopen_locker, 1), because I don't know which .so library will load in Lua script.

Thanks.

È stato utile?

Soluzione

In Linux, if you're linking liblua.a statically into your main program, you need to use -Wl,-E when linking to export the Lua API symbols; this is how the standard command line interpreter is built.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top