Jonny Saunders
Jonny Saunders

Reputation: 21

Linker not finding dependency of Lua C Module

So I am in a bit of a pickle here and am at my wits end.

Issue: I have made some lua C module that target another package's C++ shared library. Attempting to require the Lua module fails due to a dependency issue. I can find the module shared library just fine but when the linker attempts to find the library I need to link against, I get an error that looks like:

/usr/bin/lua: error loading module 'mytest' from file '/usr/lib/lua/5.3/mytest.so':
        Error loading shared library libmy_test.so: No such file or directory (needed by /usr/lib/lua/5.3/mytest.so)

The directory structure used for linking is

|-usr
|--lib
|---lua
|----5.3
|-----mytest.so
|-mockfs
|--usr
|---lib
|----libmy_test.so

My LUA_CPATH is:

/usr/lib/lua/5.3/?.so;/mockfs/usr/lib/?.so;

My LD_LIBRARY_PATH is:

/mockfs/usr/lib/

What is driving me mad is that when the libmy_test.so is in the same directory as mytest.so, there are no problems. But due to other restrictions I cannot modify the path of libmy_test.so or add a symlink without substantial refactoring and lag time for builds/regressions. I'm unsure of where to even continue here because it is definitely a "searching for the library" problem but I don't know how to get insight into what is going on under the covers to fix the issue.

Upvotes: 2

Views: 214

Answers (2)

Jonny Saunders
Jonny Saunders

Reputation: 21

So this will be unrelated to other people's Lua code. This problem description is slightly abstracted from reality as there is a lot of internal tooling from my company in front of it. I followed Paul's answer to try and get more information, LD_DEBUG was working up until I called my Lua script. Then the linker would stop giving me any information even though the require was still failing with the same error.

After a LOT of digging and my personal discovery of strace I managed to identify that the linker being used when calling require was in fact using "LD_PATH" to search for libraries instead of "LD_LIBRARY_PATH". Why this was? I still have no idea other than maybe an intentional design decision to combat some of the limitations of our internal tooling when doing cross compilation.

For anyone who experiences linker issues with Lua, LD_DEBUG and strace are very nice utilities for running down your issues.

Upvotes: 0

Paul Kulchenko
Paul Kulchenko

Reputation: 26744

Adding /mockfs/usr/lib/?.so to LUA_CPATH is not going to help, as it only works for Lua modules loaded with require (which already works in your case for mytest.so) and doesn't work for loading their dependencies (which should be handled using dynamic linking).

It's not clear if LD_LIBRARY_PATH is even used in your case; I'd set LD_DEBUG=all to get LD troubleshooting information, which should tell you what is or is not being loaded and why.

Also, even if you can't put libmy_test.so where mytest.so is located, you can still put mytest.so where libmy_test.so is and configure LUA_CPATH like you have to try making it work that way.

Upvotes: 2

Related Questions