3

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?

FortuneCookie101
  • 505
  • 1
  • 7
  • 19
  • What does `$ locate lua.hpp` show? Does `NetBeans > Tools > Options > C++ Compiler > Include Directories:` lists the directory containing `lua5.2/lua.hpp`? – Kamiccolo Oct 25 '14 at 21:41
  • locate lua.hpp (find / -name lua.hpp) doesn't return anything... this could be a problem – FortuneCookie101 Oct 25 '14 at 21:49
  • @FortuneCookie101: The compiler would have complained about a missing include file (`locate` won't find it until the database is updated with `lua5.2-dev` installed). You need to link to `liblua.a` (or `liblua.so`) using `-llua`. – siffiejoe Oct 25 '14 at 22:49
  • @siffiejoe when I try and find liblua.a or .so the search comes up with nothing, Netbeans doesn't complain about the include missing so I have no idea what's happening. If I try and manually build with -llua it complains about it not being found. – FortuneCookie101 Oct 25 '14 at 23:27
  • I just found a package called liblua5.2 so I installed that too, -llua now works however still no luck. The compiler carries on complaining about undefined references. – FortuneCookie101 Oct 25 '14 at 23:36
  • @FortuneCookie101: Sorry, Debian/Ubuntu uses `liblua5.2.so` (in package `liblua5.2-dev`), so it should have been `-llua5.2`. – siffiejoe Oct 25 '14 at 23:58
  • @FortuneCookie101: `g++ -Wall -o lx lx.cpp -llua5.2` where `lx.cpp` is your code from above compiles (and runs) fine for me ... – siffiejoe Oct 26 '14 at 00:03
  • @siffiejoe I just did the command manually like you have above and it compiled fine. When I try in Netbeans it just fails... hmm. – FortuneCookie101 Oct 26 '14 at 01:13
  • Fixed it, right click the project (left pane) -> Properties -> Build drop down -> Linker -> click three dots next to Libraries -> Add PkgConfig Library File -> Lua5.2 Sorted :) Thanks for all your help. – FortuneCookie101 Oct 26 '14 at 01:27

1 Answers1

1

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.

FortuneCookie101
  • 505
  • 1
  • 7
  • 19