C++嵌入Lua 5.2出现undefined reference to `luaL_newstate'错误(在ubuntu 14.04和Netbeans中)。

我正在尝试将Lua嵌入到C ++中,每次尝试编译时都会出现以下错误:

/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'

我已经搜索了几个小时的解决方案,但我找不到任何有用的内容。

我安装了Lua:apt-get install lua5.2 lua5.2-dev

这是我的代码:

#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;
}

我做错了什么?

点赞
用户2013953
用户2013953

首先感谢大家的帮助。

根据我的评论,如果库没有实际添加到项目中,编译器将在 Netbeans 内失败。

为了在 Netbeans 内纠正这个问题,请右键单击项目(左侧窗格)-> 属性 -> Build 下拉菜单 -> 链接器 -> 点击 Libraries 旁边的三个点 -> 添加 PkgConfig 库文件 -> Lua5.2

现在,您的程序应该可以正确编译,生活会变得美好。

2014-10-26 01:31:01