Search code examples
luaffiluajit

Lua ffi.load fails to find library without absolute path


Lua's ffi.load("library") results in a cannot open shared object file: No such file or directory error.

As a temporary solution I can provide the absolute or relative path to the library. Eg. ffi.load("./liblibrary.so").

I've read that LUA_PATH and LUA_CPATH need to be set. Eg:

export LUA_PATH="$PWD/src/?.lua"
export LUA_CPATH="$PWD/lib/?.so"

Setting LUA_PATH enables me to "require" lua files from other directories, but LUA_CPATH doesn't seem to help with loading shared objects.


Solution

  • ffi.load uses the normal dlopen mechanism of your operating system.

    On Linux, this means that you need to add your libraries path to the LD_LIBRARY_PATH variable.

    Once LD_LIBRARY_PATH is set, Lua is able to find the library.

    I found this answer helped me debug my situation by printing any matching libraries on LD_LIBRARY_PATH.

    Relevant FFI documentation