嵌入式LuaJIT未定义的引用。

我一直在尝试将Luajit嵌入(静态)到C++应用程序中,但一直未能成功。我按照Luajit网站上的步骤进行了操作,尝试过搜索相关信息并尽我所能,但却没能成功。

看起来我的问题是g++无法正确链接它,或者Luajit可能构建不正确。

这是我尝试编译的内容:

#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

int main(void)
{
    lua_State *L = luaL_newstate();
    luaL_openlibs(L);
    lua_close(L);

    return 0;
}

我得到的错误是“undefined reference”错误,几乎就像我根本没有链接Luajit一样。

main.cpp:7: undefined reference to `luaL_newstate()'
main.cpp:9: undefined reference to `luaL_openlibs(lua_State*)'
main.cpp:10: undefined reference to `lua_close(lua_State*)'

我尝试使用eclipse cdt(mingw)进行编译,并进行了以下链接选项配置:

Library Paths:        D:/includes/LuaJIT-2.0.1/src
Includes (GNU G++):   D:/includes/LuaJIT-2.0.1/src
Libraries:            luajit

而eclipse cdt似乎正打算这样编译它:

g++ -ID:/includes/LuaJIT-2.0.1/src -O0 -g3 -pedantic-errors -Wall -Wextra -Werror -Wconversion -c -fmessage-length=0 -Weffc++ -std=c++11 -o "source\\main.o" "..\\source\\main.cpp"
g++ -LD:/includes/LuaJIT-2.0.1/src -o test.exe "source\\main.o" -lluajit

我从下载页下载了最新版本的LuaJIT-2.0.1 zip,然后按照安装指南进行操作:

  • 解压它。
  • 开始运行cmd并cd到LuaJIT-2.0.1/src/
  • 运行make BUILDMODE=static,得到一个不错的“OK Successfully built LuaJIT”消息。

除此以外,在嵌入Luajit方面并没有太多的信息,即使在安装指南中也是如此。

然而,在src文件夹中搜索luaL_newstate时,在libluajit.a中找到了匹配项。

grep -r "luaL_newstate" .

./host/genminilua.lua:  lua_State *L = luaL_newstate();
./host/minilua.c:static lua_State*luaL_newstate(void){
./host/minilua.c:lua_State*L=luaL_newstate();
./lauxlib.h:LUALIB_API lua_State *(luaL_newstate) (void);
Binary file ./libluajit.a matches
./lib_aux.c:LUALIB_API lua_State *luaL_newstate(void)
./lib_aux.c:LUALIB_API lua_State *luaL_newstate(void)
./lib_aux.c:  fputs("Must use luaL_newstate() for 64 bit target\n", stderr);
Binary file ./lib_aux.o matches
./lua.h:#define lua_open()      luaL_newstate()
Binary file ./luajit.exe matches
Binary file ./luajit.o matches

我使用:

  • mingw的“g++(rev,Built by MinGW-builds project)4.8.0 20130314 (experimental)”
  • mingw的“GNU Make 3.82.90 Built for i686-pc-msys”
  • Win8 64位
点赞
用户1847592
用户1847592

将下面翻译成中文并且保留原本的 markdown 格式,

Wrap your `include` directives into `extern "C" { ... }`

把你的 include 指令放进 extern "C" { ... } 中。

2013-03-22 18:17:01
用户1127972
用户1127972

你应该在 C++ 代码中使用 #include "lua.hpp",而不是 lua.h

C API文档的顶部提到了这一点。

2015-11-06 07:10:32