C++ and Lua with SQLite

C++ and Lua with SQLite

本文关键字:SQLite with Lua and C++      更新时间:2023-10-16

我有带有Lua绑定的c++。一切都很好,但是如果我添加SQLite,我的脚本停止工作(甚至没有一个空脚本只有一个print运行)。

在我的c++代码中,我调用
luaL_openlibs( this->state );
luaopen_lsqlite3(this->state); /* sqlite */

如果我这样做,Lua脚本不再工作。

如果我删除(注释掉)luaopen_lsqlite3,脚本工作(但显然没有SQLite)。怎么了?或者我该怎么称呼?

我使用http://lua.sqlite.org/index.cgi/index

我找到了一个解决方案,这需要编辑Lua源文件。


In add

#define LUA_SQLLIBNAME  "lsqlite3"
LUAMOD_API int (luaopen_lsqlite3)(lua_State *L);

之前
LUALIB_API void (luaL_openlibs) (lua_State *L);

linit.h{LUA_SQLLIBNAME, luaopen_lsqlite3 }添加到loadedlibs数组中。得到

static const luaL_Reg loadedlibs[] = {
  {"_G", luaopen_base},
  {LUA_LOADLIBNAME, luaopen_package},
  {LUA_COLIBNAME, luaopen_coroutine},
  {LUA_TABLIBNAME, luaopen_table},
  {LUA_IOLIBNAME, luaopen_io},
  {LUA_OSLIBNAME, luaopen_os},
  {LUA_STRLIBNAME, luaopen_string},
  {LUA_BITLIBNAME, luaopen_bit32},
  {LUA_MATHLIBNAME, luaopen_math},
  {LUA_DBLIBNAME, luaopen_debug},
  {LUA_SQLLIBNAME, luaopen_lsqlite3 },
  {NULL, NULL}
};

现在,如果你调用luaL_openlibs,在Lua中调用local sqlite3 = require("lsqlite3")后,SQLite支持将在你的代码中出现