June 8, 2013 - Tagged as: en, cpp, lua.
For some reason, I couldn’t load my dynamic library(compiled to .so
) in Love2D linked with Lua 5.1.5. It was failing with undefined symbol: lua_getfield
error. Interesting point is that I could load same library from directly in Lua 5.1.5 interpreter, which is the same thing as Love2D’s Lua interpreter. They’re compiled with same headers and linked with same object files.
I wasted several hours trying to compile Love2D with LuaJIT with the hope of loading my library from Love2D. I had tried loading my library from several different Lua versions before, Lua 5.1.5, Lua 5.2.5 and LuaJIT 2.0.2, and everything had worked perfectly. So I was expecting it to work with Love2D’s Lua interpreter too.
The problem was that Love2D’s configure script was not using environment variables to look for headers/object files. I was using same environment variables to build some other programs that use LuaJIT, and only Love2D had that problem.
To overcome this, I had to install LuaJIT to /usr/
. There no way to compile Love2D with LuaJIT other than installing LuaJIT to /usr/
. Installing /usr/local/
doesn’t work. Setting environment variables(LDFLAGS
, LIBS
and CPPFLAGS
) doesn’t work.
After that, I could compile Love2D. But that wasn’t enough because then generated executable was failing with libluajit-5.1.so.2: cannot open shared object file: No such file or directory
. The reason of this error is latest version of LuaJIT generates a library file with different name: libluajit-5.1.so.2.0.2
. This makes sense, I think most C/C++ libraries use same naming conventions.
Thankfully, renaming it works. Just rename that so file with required file.
I’m working on some Lua libraries written in C++, and testing gets painful when you can’t be sure which libraries you’re library is linked against. For this reason I don’t install Lua to /usr
or /usr/local
, and manually specify header/object file locations while compiling my library. So I removed all LuaJIT files from /usr/lib
, /usr/include
, etc. But to make Love2D work, I need libluajit-5.1.so.2
. So I created a lib
folder in my home dir, moved LuaJIT file there, and add that folder to $LD_LIBRARY_PATH
environment variable. With this, I had a Love2D working with latest LuaJIT.
Hope this helps other people who want LuaJIT enabled Love2D on Linux.