3

I have this piece of code (file luascript.cpp):

bool LuaInterface::initState()
{
    m_luaState = luaL_newstate();
    if(!m_luaState)
        return false;

    luaL_openlibs(m_luaState);
#ifdef __LUAJIT__
    luaJIT_setmode(m_luaState, 0, LUAJIT_MODE_ENGINE | LUAJIT_MODE_ON);
#endif

    registerFunctions();
    if(!loadDirectory(getFilePath(FILE_TYPE_OTHER, "lib/"), false, true))
        std::clog << "[Warning - LuaInterface::initState] Cannot load " << getFilePath(FILE_TYPE_OTHER, "lib/") << std::endl;

    lua_newtable(m_luaState);
    lua_setfield(m_luaState, LUA_REGISTRYINDEX, "EVENTS");
    m_runningEvent = EVENT_ID_USER;
    return true;
}

the declaration (file luajit.h):

LUA_API int luaJIT_setmode(lua_State *L, int idx, int mode);

and the error is:

1>luascript.obj : error LNK2019: unresolved external symbol _luaJIT_setmode referenced in function "public: virtual bool __thiscall LuaInterface::initState(void)" (?initState@LuaInterface@@UAE_NXZ)
1>C:\Users\GUIAKI\Documents\trunk.r5918\vc10\Debug\tfs.exe : fatal error LNK1120: 1 unresolved externals

How can I solve it?

Guilherme Garcia
  • 161
  • 4
  • 14

2 Answers2

3

Simply remove that line.

You can't link against plain Lua, if you keep it. And if you link against LuaJIT, the JIT compiler is enabled by default, anyway. That line of code is utterly pointless.

Mike Pall
  • 1,475
  • 9
  • 8
  • I had done that before, by doing that I can successfully compile the .exe, but when I execute it, instantly it crashes, what I can do? – Guilherme Garcia Jan 28 '13 at 13:36
2

Seems like you forgot to link the library being part of "luaJIT" (never heard of it or used it).

There should be a lib file you'll have to add to your project as an additional dependency (linker settings).

Also keep in mind to include the correct headers ("lua.hpp" for C++, "luajit.h" for C).

Mario
  • 35,726
  • 5
  • 62
  • 78
  • Well, I have looked up for luaJIT, there isn't indeed a luaJIT.lib, there is the lua51.lib, which already includes the luaJIT, It has been already added, What I done now that I haven't done before is that I added #include "luajit.h" to the luascript.cpp, but still I have the same error, what can I do? – Guilherme Garcia Jan 27 '13 at 17:46
  • According to their homepage you're supposed to include "lua.hpp" in your code. "luajit.h" is for C code only. So I guess you're trying to use the wrong bindings unintentionally. – Mario Jan 27 '13 at 17:53
  • Then make sure you're linking everything, e.g. determine whether the "lua51.lib" is really everything. I'd say it's the actual interpreter only (without the jit stuff). – Mario Jan 27 '13 at 19:06