Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ embed lua 5.2 undefined reference to `luaL_newstate' (ubuntu 14.04, Netbeans)

Tags:

c++

netbeans

lua

So I'm trying to embed Lua into C++ and every time I try and 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 searching for a solution for hours but I can't find anything helpful.

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?

like image 505
FortuneCookie101 Avatar asked Jan 20 '26 04:01

FortuneCookie101


1 Answers

First of all thanks for everyone's help on this.

As per my comment, the compiler will fail inside Netbeans unless the library is actually added to the project.

To rectify this inside Netbeans right click the project (left pane) -> Properties -> Build drop down -> Linker -> click three dots next to Libraries -> Add PkgConfig Library File -> Lua5.2

Your program should now compile correctly and life will be good.

like image 119
FortuneCookie101 Avatar answered Jan 22 '26 18:01

FortuneCookie101



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!