Lua 5.2 C api中的语法更改

我在尝试编译《Lua 编程》这本书中提供的示例,但它只适用于 Lua 5.1,如何在 5.2 上进行编译?

这是我使用的代码:

#include <stdio.h>
#include <string.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

int main (void) {
  char buff[256];
  int error;
  lua_State *L = lua_open();   /* opens Lua */
  luaL_openlibs(L);
  while (fgets(buff, sizeof(buff), stdin) != NULL) {
    error = luaL_loadbuffer(L, buff, strlen(buff), "line") ||
      lua_pcall(L, 0, 0, 0);
    if (error) {
      fprintf(stderr, "%s", lua_tostring(L, -1));
      lua_pop(L, 1);  /* pop error message from the stack */
    }
  }
  lua_close(L);
  return 0;
}

使用 gcc test01.c -I/usr/include/lua5.2 -L/usr/lib/x86_64-linux-gnu -llua5.2 编译后,我得到以下错误:

test01.c: In function ‘main’:
test01.c:10:18: warning: initialization makes pointer from integer without a cas
t [enabled by default]
   lua_State *L = lua_open();   /* opens Lua */
                  ^
/tmp/ccyPRlV3.o: In function `main':
test01.c:(.text+0x21): undefined reference to `lua_open'
collect2: error: ld returned 1 exit status

非常感谢。

点赞
用户1009479
用户1009479

luaopen()不再使用,取而代之的是luaL_newstate,你可以使用luaL_newstate来创建一个带有标准分配函数的状态:

lua_State *L = luaL_newstate();    /* 打开 Lua */
luaL_openlibs(L);                  /* 打开标准库 */

自Lua 5.1以来,此API已更改。

2014-08-11 08:39:50
用户1165590
用户1165590

尝试:

lua_State *L = luaL_newstate();
2014-08-11 08:41:25