5

I was trying to compile the example provided in the book Programming in Lua

But only works for lua 5.1, What are the steps to do it on 5.2?

This is the code I am using

#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;
}

After compiling with gcc test01.c -I/usr/include/lua5.2 -L/usr/lib/x86_64-linux-gnu -llua5.2 I get the following errors:

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

Thank you in advance.

rocastocks
  • 153
  • 1
  • 10
  • Apparently the `lua_open` call was removed already in version 5.1. See e.g. [the "Incompatibilities with the Previous Version" section of the 5.1 reference manual](http://www.lua.org/manual/5.1/manual.html#7). – Some programmer dude Aug 11 '14 at 08:39
  • 1
    In Lua 5.2.2 they use lua_State *L = lua_newstate(); – Baj Mile Aug 11 '14 at 08:41
  • @BajMile luaL_newstate; see why it's important for interfaces to be immutable? There should be a compatibility API out of the box. – Dmytro Jun 09 '18 at 10:00

2 Answers2

9

luaopen() is not used anymore, it's replaced by luaL_newstate, you can use luaL_newstate to create a state with a standard allocation function:

lua_State *L = luaL_newstate();    /* opens Lua */
luaL_openlibs(L);                  /* opens the standard libraries */

This API is changed since Lua 5.1

Jan Turoň
  • 31,451
  • 23
  • 125
  • 169
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
3

Try:

lua_State *L = luaL_newstate();
Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116
Baj Mile
  • 750
  • 1
  • 8
  • 17