Error while loading shared libraries

The product is a full embedded Linux operating system. I am trying to add a module to a layer provided by a component provider. For development I am using VS-Code on Ubuntu 18.04. The project is in /home/me/develop/source/ProductX/src/modules/companyGlue and the test directory is underneath it.

I can build a test program but when I run it I get:

/home/me/develop/source/ProductX/.build-pc/src/modules/companyGlue/test/glue_test: error while loading shared libraries: libjson-sax.so: cannot open shared object file: No such file or directory
[1] + Done “/usr/bin/gdb” --interpreter=mi --tty=${DbgTerm} 0<“/tmp/Microsoft-MIEngine-In-xdvmrrfm.on5” 1>“/tmp/Microsoft-MIEngine-Out-2pc4z9vb.qyg”

src/external-pc-only/json-sax/CMakeLists.txt contains
add_library(json-sax SHARED ${json-sax_SOURCES})
install(TARGETS json-sax DESTINATION ${CMAKE_INSTALL_LIBDIR})

After building, .build-pc/src/external-pc-only/json-sax contains:
libjson-sax.so

The project result, libnsdk_companyglue.so, contains the full path /home/me/…/json-sax.

Is there anything I can do to CMakeLists.txt or test/CMakeLists.txt to help glue_test find the json-sax library?

All CMake can do is put it in the right place. This depends on how libnsdk_companyglue.so ends up looking for the file. You can write code that queries where the library itself is on disk[1] then look relative to that.

[1]NB: This fails miserably for static builds.

I ran strace on my test program. It shows .build-pc/src/external-pc-only/json-sax being searched several times while looking for other libraries. The directory was not searched when looking for libjson-sax.so. It looks like an extremely inefficient and problematic way of searching for libraries.

Using the LD_DEBUG=libs environment variable (for Linux) may be more suitable here.