سؤال

I'm unsuccessfully trying to embed (statically) luajit into a c++ application. I have followed the steps from the luajit site to no avail, googled on the subject and got no relevant information and tried every thing I could think of.

It appears as if the problem I'm running in is that g++ doesn't quite link it properly, or perhaps luajit ins't building properly.

This is what I'm trying to compile:

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

And the error I'm getting is a undefined reference error almost as if I hadn't linked luajit at all.

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*)'

I'm trying to compile it with eclipse cdt (mingw) after setting the linking options as following:

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

And eclipse cdt appears to be trying to compile it as following:

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

I have downloaded LuaJIT-2.0.1 zip (latest version) from their download page and followed the installation guide as following:

  • Unziped it.
  • Fired up a cmd and cded to LuaJIT-2.0.1/src/
  • Ran make BUILDMODE=static and got a nice OK Successfully built LuaJIT message

Other than that there ins't a lot of information on embedding luajit, not even in the installation guide.

However grepping the src folder for luaL_newstate returns a match in the 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

I'm using:

  • mingw's g++ (rev, Built by MinGW-builds project) 4.8.0 20130314 (experimental)
  • mingw's GNU Make 3.82.90 Built for i686-pc-msys
  • Win8 64bit
هل كانت مفيدة؟

المحلول

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

نصائح أخرى

You should use #include "lua.hpp" in C++ code, not lua.h.

This is mentioned at the top of the documentation for the C API.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top