# Error while loading shared libraries

**URL:** https://discourse.cmake.org/t/error-while-loading-shared-libraries/3239
**Category:** Usage
**Tags:** os:linux
**Created:** [April 29, 2021, 8:47pm UTC](https://discourse.cmake.org/t/error-while-loading-shared-libraries/3239 "2021-04-29T20:47:35Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![JoeAB](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/9fc29f/32.png) [@JoeAB](https://discourse.cmake.org/u/JoeAB)
#### Post date: [April 29, 2021, 8:47pm UTC](https://discourse.cmake.org/t/error-while-loading-shared-libraries/3239/1 "2021-04-29T20:47:36Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [April 30, 2021, 6:32pm UTC](https://discourse.cmake.org/t/error-while-loading-shared-libraries/3239/2 "2021-04-30T18:32:08Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![JoeAB](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/9fc29f/32.png) [@JoeAB](https://discourse.cmake.org/u/JoeAB)
#### Post date: [April 30, 2021, 7:06pm UTC](https://discourse.cmake.org/t/error-while-loading-shared-libraries/3239/3 "2021-04-30T19:06:09Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [April 30, 2021, 7:53pm UTC](https://discourse.cmake.org/t/error-while-loading-shared-libraries/3239/4 "2021-04-30T19:53:46Z")

</div>

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