# cling::DynamicLibraryManager::loadLibrary severely broken

**URL:** <https://root-forum.cern.ch/t/cling-loadlibrary-severely-broken/45924>\
**Category:** ROOT\
**Created:** [July 20, 2021, 9:54am UTC](https://root-forum.cern.ch/t/cling-loadlibrary-severely-broken/45924 "2021-07-20T09:54:20Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [July 20, 2021, 9:54am UTC](https://root-forum.cern.ch/t/cling-loadlibrary-severely-broken/45924/1 "2021-07-20T09:54:21Z")

</div>

@Axel This is Ubuntu 20.04 / x86\_64 / gcc 9.3.0 and ROOT 6.24/02.

I have a bunch of shared libraries which I try to load using:

```cpp
root [0] gSystem->AddDynamicPath("/.../VTK-9.0/lib")
root [1] gSystem->Load("libvtkCommonCore-9.0")
cling::DynamicLibraryManager::loadLibrary(): libvtksys-9.0.so.1: cannot open shared object file: No such file or directory
(int) -1

```

However, the “missing” library DOES exist in the same directory, as can be proven by:

```cpp
root [2] gSystem->Load("libvtksys-9.0.so.1")
(int) 0

```

Moreover, if I first:

```bash
export LD_LIBRARY_PATH="/.../VTK-9.0/lib:${LD_LIBRARY_PATH}"

```

then it works:

```cpp
root [0] gSystem->Load("libvtkCommonCore-9.0")
(int) 0

```

I think, ROOT needs to be able to “automatically” load any of these:

```bash
[...] $ ls -l libvtkCommonCore-9.0*
... libvtkCommonCore-9.0.so -> libvtkCommonCore-9.0.so.1
... libvtkCommonCore-9.0.so.1 -> libvtkCommonCore-9.0.so.9.0.3
... libvtkCommonCore-9.0.so.9.0.3
[...] $ ls -l libvtksys-9.0*
... libvtksys-9.0.so -> libvtksys-9.0.so.1
... libvtksys-9.0.so.1 -> libvtksys-9.0.so.9.0.3
... libvtksys-9.0.so.9.0.3

```

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [July 20, 2021, 2:13pm UTC](https://root-forum.cern.ch/t/cling-loadlibrary-severely-broken/45924/2 "2021-07-20T14:13:16Z")

</div>

Hi Wile\_E,

`gSystem->AddDynamicPath()` does not add to `LD_LIBRARY_PATH`, it just adds to the directories ROOT searches as part of `gSystem->Load()`.

When ROOT tries to `dlopen` `vtkCommon`, that depends on a different library, and `dlopen` not ROOT!) cannot find that dependency. When you ask ROOT to load it it’s all fine because of your call to `AddDynamicPath` - but `dlopen` doesn’t know about that.

So the right thing indeed is to export `LD_LIBRARY_PATH`, either ouside or inside ROOT with `gSystem->SetEnvVar()`.

Cheers, Axel.

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [July 20, 2021, 3:13pm UTC](https://root-forum.cern.ch/t/cling-loadlibrary-severely-broken/45924/3 "2021-07-20T15:13:19Z")

</div>

Should/Could `AddDynamicPath` also update the `LD_LIBRARY_PATH` variable (via `TSystem::setenv`?)

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [July 20, 2021, 6:01pm UTC](https://root-forum.cern.ch/t/cling-loadlibrary-severely-broken/45924/4 "2021-07-20T18:01:53Z")

</div>

IMO no - that’s a backward incompatibility: people will set `LD_LIBRARY_PATH` (possibly through `TSystem`) to set `LD_LIBRARY_PATH`, and call `AddDynamicPath` to allow ROOT to find libraries. Maybe it’s enough to call this out in the doc?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [July 20, 2021, 7:31pm UTC](https://root-forum.cern.ch/t/cling-loadlibrary-severely-broken/45924/5 "2021-07-20T19:31:33Z")

</div>

So, I tried:

```cpp
root [0] TString d = "/.../VTK-9.0/lib";
root [1] gSystem->Setenv("LD_LIBRARY_PATH", TString(gSystem->Getenv("LD_LIBRARY_PATH")) + ":" + d);
root [2] gSystem->Load("libvtkCommonCore-9.0")
Error in <TUnixSystem::FindDynamicLibrary>: libvtkCommonCore-9.0[.so | .dll | .dylib | .sl | .dl | .a] does not exist in ...
(int) -1

```

And then I tried:

```cpp
root [0] TString d = "/.../VTK-9.0/lib";
root [1] gSystem->Setenv("LD_LIBRARY_PATH", TString(gSystem->Getenv("LD_LIBRARY_PATH")) + ":" + d);
root [2] gSystem->AddDynamicPath(d);
root [3] gSystem->Load("libvtkCommonCore-9.0")
cling::DynamicLibraryManager::loadLibrary(): libvtksys-9.0.so.1: cannot open shared object file: No such file or directory
(int) -1

```

In other words, the loading of shared libraries is really terribly broken.

The “[`TSystem::AddDynamicPath`](https://root.cern/doc/master/classTSystem.html#ab24a240660619b366d5feae6bc33479b)” is useless if it cannot properly manage dynamic linking.

When ROOT starts, the contents of the “`LD_LIBRARY_PATH`” environment variable is directly copied into the “`gSystem->GetDynamicPath()`” but, as demonstrated above, later modifications done from inside of the running application are improperly handled. Please fix it.

The contents of the “`gSystem->GetDynamicPath()`” should be passed to the “`dlopen`”.

---

<div class="post-metadata">

**Author:** ![Axel](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/axel/32/11084_2.png) [@Axel](https://root-forum.cern.ch/u/Axel)\
**Post date:** [July 21, 2021, 4:19pm UTC](https://root-forum.cern.ch/t/cling-loadlibrary-severely-broken/45924/6 "2021-07-21T16:19:30Z")

</div>

> [@Wile\_E\_Coyote](#):
>
> The contents of the “`gSystem->GetDynamicPath()`” should be passed to the “`dlopen`”.

That’s your case here, but might completely destroy the setup of experiments. We’re all the center of the world, but there are multiple centers 😉

If you read what I explained above then the behavior you demonstrate is what I’d expect; thanks for proving it. In general, if you want dlopen to find a library in a custom / non-default path then you _have_ to set `LD_LIBRARY_PATH`. Indeed, the ROOT library search path is not updated _from_ `LD_LIBRARY_PATH` once set; we could do that, I see no issue with that. @pcanal what’s your take?

---

<div class="post-metadata">

**Author:** ![pcanal](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/pcanal/32/12439_2.png) [@pcanal](https://root-forum.cern.ch/u/pcanal)\
**Post date:** [July 21, 2021, 6:16pm UTC](https://root-forum.cern.ch/t/cling-loadlibrary-severely-broken/45924/7 "2021-07-21T18:16:30Z")

</div>

It appears that changing `LD_LIBRARY_PATH` after the start of the process in not working. Reading through google it “appears” that `dlopen` gets configured at process startup. If that is correct, what Wile request is “impossible” to implement (cheaply). Ideally I agree that `SetDynamicPath` ought to have the same effect as setting `LD_LIBRARY_PATH` (i.e find both top level and inner libraries) but implementing this would required to re-implement the dependency walk in dlopen (i.e. find the top library, find it dependency and open the most derived dependencies first).

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [July 21, 2021, 8:25pm UTC](https://root-forum.cern.ch/t/cling-loadlibrary-severely-broken/45924/8 "2021-07-21T20:25:30Z")

</div>

I have found a brutal fix that may be of interest to other people.

It works on Linux for shared libraries in the default ELF format (I do not know any fix for macOS and / or Windows, sorry).

The trick is to add “`$ORIGIN`” to their “`RUNPATH`”.

After building the VTK software and executing “make install”, I have around 160 shared libraries in a single directory ("`/.../VTK-9.0/lib`" in my previous posts here). By default, these libraries do not have any “`RUNPATH`” set, so I could simply execute:

```bash
patchelf --set-rpath "\$ORIGIN" *.so

```

Note: the above command unconditionally sets the new “`RUNPATH`” so one should first make sure that it is really empty:

```bash
readelf -d *.so | egrep "File:|RPATH|RUNPATH"

```

If for “`libSomeLibrary.so`” one sees some non-empty “`old_value`” then for this library one should execute:

```bash
patchelf --set-rpath "old_value:\$ORIGIN" libSomeLibrary.so

```

---

<div class="post-metadata">

**Author:** ![James-Carroll](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/james-carroll/32/27108_2.png) [@James-Carroll](https://root-forum.cern.ch/u/James-Carroll)\
**Post date:** [July 21, 2021, 10:02pm UTC](https://root-forum.cern.ch/t/cling-loadlibrary-severely-broken/45924/9 "2021-07-21T22:02:40Z")

</div>

You could set rpath at the build system level with the GCC flags `-Wl,-rpath,/path/to/dir`, which is less hacky than patching the binary directly.

But the actual feature of having this isn’t bad at all, it certainly isn’t a “brutal fix” 😉 rpath is a great solution here and LD\_LIBRARY\_PATH, although common, isn’t always the best choice.

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [August 4, 2021, 10:02pm UTC](https://root-forum.cern.ch/t/cling-loadlibrary-severely-broken/45924/10 "2021-08-04T22:02:45Z")

</div>

Closed on August 4, 2021, 10:02pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
