C ++ embed lua 5.2 undefined reference to `luaL_newstate '(ubuntu 14.04, Netbeans)
So I am trying to implement Lua in C ++ and every time I try to compile I get this error:
/root/NetBeansProjects/test/main.cpp:20: undefined reference to `luaL_newstate'
/root/NetBeansProjects/test/main.cpp:31: undefined reference to `lua_settop'
/root/NetBeansProjects/test/main.cpp:35: undefined reference to `luaL_loadfilex'
/root/NetBeansProjects/test/main.cpp:35: undefined reference to `lua_pcallk'
/root/NetBeansProjects/test/main.cpp:38: undefined reference to `lua_close'
I've been looking for a solution for hours, but I can't find anything useful.
I installed Lua: apt-get install lua5.2 lua5.2-dev
Here's my code:
#include <cstdlib>
#include <iostream>
#include <string.h>
#include <string>
#include <lua5.2/lua.hpp>
using namespace std;
int main() {
// create new Lua state
lua_State *lua_state;
lua_state = luaL_newstate();
// load Lua libraries
static const luaL_Reg lualibs[] ={
{ "base", luaopen_base},
{ NULL, NULL}
};
const luaL_Reg *lib = lualibs;
for (; lib->func != NULL; lib++) {
lib->func(lua_state);
lua_settop(lua_state, 0);
}
// run the Lua script
luaL_dofile(lua_state, "test.lua");
// close the Lua state
lua_close(lua_state);
return 0;
}
What am I doing wrong?
+3
source to share
1 answer
First of all, thanks for helping everyone.
As per my comment, the compiler won't work inside Netbeans, unless the library is added to the project.
To fix this inside Netbeans, right click on the project (left pane) → Properties → Dropdown → Linker → click the three dots next to Libraries → Add PkgConfig Library File → Lua5.2
Your program should now compile correctly and life will be good.
+1
source to share