---
# System prepended metadata

title: Loading libstd mysteries

---

**Error**:

```bash=
~/dev/torchcodec (main*) » python test/decoders/manual_smoke_test.py             nicolashug@nicolashug-fedora-PF372ZT9
Traceback (most recent call last):
  File "/home/nicolashug/dev/torchcodec/test/decoders/manual_smoke_test.py", line 4, in <module>
    import torchcodec
  File "/home/nicolashug/dev/torchcodec/src/torchcodec/__init__.py", line 1, in <module>
    from . import decoders, samplers  # noqa
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/nicolashug/dev/torchcodec/src/torchcodec/samplers/__init__.py", line 5, in <module>
    from .video_clip_sampler import *  # noqa
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/nicolashug/dev/torchcodec/src/torchcodec/samplers/video_clip_sampler.py", line 12, in <module>
    from torchcodec.decoders._core import (
  File "/home/nicolashug/dev/torchcodec/src/torchcodec/decoders/_core/__init__.py", line 5, in <module>
    from .video_decoder_ops import *  # noqa
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/nicolashug/dev/torchcodec/src/torchcodec/decoders/_core/video_decoder_ops.py", line 10, in <module>
    torch.ops.load_library(_get_extension_path("libtorchcodec"))
  File "/home/nicolashug/.miniconda3/envs/codec/lib/python3.11/site-packages/torch/_ops.py", line 1278, in load_library
    ctypes.CDLL(path)
  File "/home/nicolashug/.miniconda3/envs/codec/lib/python3.11/ctypes/__init__.py", line 376, in __init__
    self._handle = _dlopen(self._name, mode)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^
OSError: /home/nicolashug/.miniconda3/envs/codec/bin/../lib/libstdc++.so.6: version `GLIBCXX_3.4.32' not found (required by /home/nicolashug/dev/torchcodec/src/torchcodec/libtorchcodec.so)

```

**Problem**: I compiled and linked torchcodec against `/usr/lib64/libstdc++.so.6` but at runtime the one that gets loaded is  `/home/nicolashug/.miniconda3/envs/codec/bin/../lib/libstdc++.so.6`, and it's missing some version symbols.


**IT IS NOT PYTHON'S FAULT IT IS OPENCV!!!!!!!!!!!!!**

First: `torchcodec` WORKS:

```
~/dev/torchcodec (main*) » LD_DEBUG=libs python -c "import torchcodec" 2>&1 | grep libstd                                         130 ↵ nicolashug@nicolashug-fedora-PF372ZT9
    133672:	find library=libstdc++.so.6 [0]; searching
    133672:	  trying file=/home/nicolashug/.miniconda3/envs/codec/lib/python3.11/site-packages/torch/lib/libstdc++.so.6
    133672:	  trying file=/lib64/libstdc++.so.6
    133672:	calling init: /lib64/libstdc++.so.6
    133672:	calling fini: /lib64/libstdc++.so.6 [0]
```

There's no error here, I can import `torchcodec` without and error and ``/usr/lib64/libstdc++.so.6`` gets properly loaded.

So it's not Python who is loading the wrong `/home/nicolashug/.miniconda3/envs/codec/bin/../lib/libstdc++.so.6` (well... we'll see).

The loading of the wrong `/home/nicolashug/.miniconda3/envs/codec/bin/../lib/libstdc++.so.6`  comes from opencv:

```
~/dev/torchcodec (main*) » LD_DEBUG=libs python -c "import cv2" 2>&1 | grep libstd                                                      nicolashug@nicolashug-fedora-PF372ZT9
    133766:	find library=libstdc++.so.6 [0]; searching
    133766:	  trying file=/home/nicolashug/.miniconda3/envs/codec/lib/python3.11/site-packages/cv2/../opencv_python.libs/libstdc++.so.6
    133766:	  trying file=/home/nicolashug/.miniconda3/envs/codec/bin/../lib/libstdc++.so.6
    133766:	calling init: /home/nicolashug/.miniconda3/envs/codec/bin/../lib/libstdc++.so.6
    133766:	calling fini: /home/nicolashug/.miniconda3/envs/codec/bin/../lib/libstdc++.so.6 [0]
```

And in fact, if I `import torchcodec` BEFORE `import cv2` in the smoke test, the smoke test runs fine!!

```diff=
~/dev/torchcodec (main*) » git diff -p | \cat                                                                                           nicolashug@nicolashug-fedora-PF372ZT9
diff --git a/test/decoders/manual_smoke_test.py b/test/decoders/manual_smoke_test.py
index 07869c5..9690d99 100644
--- a/test/decoders/manual_smoke_test.py
+++ b/test/decoders/manual_smoke_test.py
@@ -1,7 +1,7 @@
 import os
 
-import cv2
 import torchcodec
+import cv2
 
 decoder = torchcodec.decoders._core.create_from_file(
     os.path.dirname(__file__) + "/resources/nasa_13013.mp4"

```

```
~/dev/torchcodec (main*) » python test/decoders/manual_smoke_test.py && echo $?                                                   130 ↵ nicolashug@nicolashug-fedora-PF372ZT9
0
```

**So why is opencv loading the conda libstd?**

This is still somewhat of a mystery to me but running

```bash=
LD_DEBUG=all  python -c "import cv2" > all 2>&1
```

we see

```
    944879:	file=libstdc++.so.6 [0];  needed by /home/nicolashug/.miniconda3/envs/codec/lib/python3.11/site-packages/cv2/cv2.abi3.so [0]
    944879:	find library=libstdc++.so.6 [0]; searching
    944879:	 search path=/home/nicolashug/.miniconda3/envs/codec/lib/python3.11/site-packages/cv2/../opencv_python.libs		(RPATH from file /home/nicolashug/.miniconda3/envs/codec/lib/python3.11/site-packages/cv2/cv2.abi3.so)
    944879:	  trying file=/home/nicolashug/.miniconda3/envs/codec/lib/python3.11/site-packages/cv2/../opencv_python.libs/libstdc++.so.6
    944879:	 search path=/home/nicolashug/.miniconda3/envs/codec/bin/../lib		(RPATH from file python)
    944879:	  trying file=/home/nicolashug/.miniconda3/envs/codec/bin/../lib/libstdc++.so.6
```

in particular `(RPATH from file python)` is the part that is weird and I cannot explain it.
I don't understand why Python's `rpath` is used in the search path here.

Note that when doing just `import torchcodec`, python's rpath isn't used in the search path is

```
 file=libstdc++.so.6 [0]; needed by /home/nicolashug/.miniconda3/envs/codec/lib/python3.11/site-packages/torch/lib/libtorch_python.so [0]
 find library=libstdc++.so.6 [0]; searching
 search path=/home/nicolashug/.miniconda3/envs/codec/lib/python3.11/site-packages/torch/lib (RUNPATH from file /home/nicolashug/.miniconda3/envs/codec/lib/python3.11/site-packages/torch/lib/libtorch_global_deps.so)
 trying file=/home/nicolashug/.miniconda3/envs/codec/lib/python3.11/site-packages/torch/lib/libstdc++.so.6
 search cache=/etc/ld.so.cache
 trying file=/lib64/libstdc++.so.6
```

despite the fact that python's rpath *is indeed* used to load other SOs previously during execution, e.g. this one is loaded way before `libstd`:

```
 file=libdl.so.2 [0]; needed by python [0]
 find library=libdl.so.2 [0]; searching
 search path=/home/nicolashug/.miniconda3/envs/codec/bin/../lib (RPATH from file python)
 trying file=/home/nicolashug/.miniconda3/envs/codec/bin/../lib/libdl.so.2
 search cache=/etc/ld.so.cache
 trying file=/lib64/libdl.so.2
```