Search code examples
csqlitelualuasql

How to embed luasql.sqlite3 in a statically linked C program?


The luasql.sqlite3 module has been compiled into my C program successfully, statically linked. But, it seems the module has not been registered yet. The call of require 'luasql.sqlite3' always fails in Lua scripts.

Some other modules call luaL_register to register themselves. But luaL_register is not called in luaopen_luasql_sqlite3. How do I register luasql.sqlite3 in this case?

I use Lua-5.1.5.

The source code of luaopen_luasql_sqlite3 is at the bottom


Solution

  • Here is the way of putting luaopen_ functions into the package.preload table.

    lua_getfield(L, LUA_GLOBALSINDEX, "package");
    lua_getfield(L, -1, "preload");
    lua_pushcfunction(L, luaopen_socket_core);
    lua_setfield(L, -2, "socket.core");