tags:

views:

73

answers:

3
#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?

+1  A: 

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.

dutt
I thought I did. Linker --> Input --> Additional Dependencies --> lua.lib
Camoy
DumbCoder
+4  A: 

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)

Necrolis
extern "C"{#include <lua.h>#include <lauxlib.h>#include <lualib.h>}#include <stdio.h>#include <string.h> // Like so? It gets a different error this time, Code Generation Failed.
Camoy
@Camoy: What errors accompany the failure? Did you rebuild all to avoid polluted objs?
Necrolis
A: 

The Lua files are in C so you have to use

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

You are just getting linker errors.

Matt