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

This seems to propagate several errors such as:

error LNK2019: unresolved external symbol "char const * __cdecl lua_tolstring(struct lua_State *,int,unsigned int *)" (?lua_tolstring@@YAPBDPAUlua_State@@HPAI@Z) referenced in function _main main.obj

What's wrong?

5
If you're doing C++, then you should include: /lua-5.1.4/etc/lua.hppAraK
Sorry but where is that in the code you presented above?AraK
I misread :). Even so, with #include <lua.hpp>, it errors "code generation fail"Q2Ftb3k

5 Answers

6
votes

You need to wrap the lua headers in extern "C" to get the correct symbol linkages, as well as linking to the lib(if your not compiling it into the project)

6
votes

Lua 5.1 has lua.hpp:

// lua.hpp
// Lua header files for C++
// <<extern "C">> not supplied automatically because Lua also compiles as C++

extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}

Just #include <lua.hpp>.

2
votes

Encountered this linking error and I Had to change the

#define LUA_API extern

to

#define LUA_API extern "C"

I'm using Lua 5.1 BTW

1
votes

Probably nothing wrong with your code, you have a linking problem, it can't find the function definition for lua_tolstring. Add the lua library when linking and you should be fine.

0
votes

The Lua files are in C so you have to use

extern "C" { #include "luafiles.cpp" }

You are just getting linker errors.