Reputation: 688
#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?
Upvotes: 1
Views: 2836
Reputation: 21
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
Upvotes: 2
Reputation: 1914
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>
.
Upvotes: 6
Reputation: 1211
The Lua files are in C so you have to use
extern "C" { #include "luafiles.cpp" }
You are just getting linker errors.
Upvotes: 0
Reputation: 26171
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)
Upvotes: 6
Reputation: 8209
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.
Upvotes: 1