在C应用程序中加载Lua库。

我想让我的 C 程序能够调用 Lua 函数,但我不知道如何加载 Lua 库。

目前我尝试从 lua.org 下载所有 Lua 源代码,然后将它们放在与我的 C 程序相同的文件夹中。然后在 C 代码中将它们包含进去。

现在我的代码如下所示:

#include "lua.h"
#include "lauxlib.h"

int main()
{
    lua_State *L = luaL_newstate();
    luaL_openlibs(L);
    luaL_dofile(L, "test.lua");
    return 0;
}

编译时出现的错误信息:

test.c: In function ‘main’: test.c:10:5: warning: implicit declaration of function ‘luaL_openlibs’ [->Wimplicit-function-declaration] luaL_openlibs(L); ^ In file included from test.c:5:0: lauxlib.h:122:24: warning: value computed is not used [-Wunused-value] (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0)) ^ test.c:11:5: note: in expansion of macro ‘luaL_dofile’ luaL_dofile(L, "test.lua"); ^ /tmp/ccHJTAY8.o: In function main': test.c:(.text+0x9): undefined reference to luaL_newstate' test.c:(.text+0x1e): undefined reference to luaL_openlibs' test.c:(.text+0x34): undefined reference to luaL_loadfilex' test.c:(.text+0x5f): undefined reference to `lua_pcallk' collect2: error: ld returned 1 exit status

点赞