# find\_library so and all symlinks

**URL:** <https://discourse.cmake.org/t/find-library-so-and-all-symlinks/4160>\
**Category:** Code\
**Created:** [September 28, 2021, 3:02pm UTC](https://discourse.cmake.org/t/find-library-so-and-all-symlinks/4160 "2021-09-28T15:02:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmak](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@cmak](https://discourse.cmake.org/u/cmak)\
**Post date:** [September 28, 2021, 3:02pm UTC](https://discourse.cmake.org/t/find-library-so-and-all-symlinks/4160/1 "2021-09-28T15:02:15Z")

</div>

How to find a library (`find_library`) and all associated symlinks to that library? E.g. a library typically looks like

```auto
libfoo.so
libfoo.so.1
libfoo.so.1.0.0 

```

where the latter two are symlinks. Is there a better way than stating a `find_library` 3 times?

---

<div class="post-metadata">

**Author:** ![hsattler](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@hsattler](https://discourse.cmake.org/u/hsattler)\
**Post date:** [September 28, 2021, 6:16pm UTC](https://discourse.cmake.org/t/find-library-so-and-all-symlinks/4160/2 "2021-09-28T18:16:25Z")

</div>

When you found the .so, you can resolve the symlink to get the .so.1.0.0 and then read the name that ld will use from the ELF headers.

At least that will be more consistent than 3 find\_library calls

---

<div class="post-metadata">

**Author:** ![cmak](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@cmak](https://discourse.cmake.org/u/cmak)\
**Post date:** [September 29, 2021, 9:48am UTC](https://discourse.cmake.org/t/find-library-so-and-all-symlinks/4160/3 "2021-09-29T09:48:13Z")

</div>

Not sure I understand how do that with cmake. Typically it would look like this;

```bash
libfoo.so -> libfoo.so.1.0.0*
libfoo.so.1 -> libfoo.so.1.0.0*
libfoo.so.1.0.0*

```

Finding `libfoo.so` is easy. Some libraries are installed with one symlink, other with two symlinks. Also I want to install the symlink and not a copy of the symlink.

---

<div class="post-metadata">

**Author:** ![hsattler](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/h/59ef9b/32.png) [@hsattler](https://discourse.cmake.org/u/hsattler)\
**Post date:** [September 29, 2021, 3:25pm UTC](https://discourse.cmake.org/t/find-library-so-and-all-symlinks/4160/4 "2021-09-29T15:25:57Z")

</div>

What exactly do you want to achieve? Maybe there’s a simpler way…

---

<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:** [September 29, 2021, 5:47pm UTC](https://discourse.cmake.org/t/find-library-so-and-all-symlinks/4160/5 "2021-09-29T17:47:15Z")

</div>

Probably not as efficient as what you’re looking for, but `find -L $dirname -maxdepth 1 -samefile $libname.so` should at least get the same answer.

---

<div class="post-metadata">

**Author:** ![cmak](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/c/eb8c5e/32.png) [@cmak](https://discourse.cmake.org/u/cmak)\
**Post date:** [September 30, 2021, 6:33am UTC](https://discourse.cmake.org/t/find-library-so-and-all-symlinks/4160/6 "2021-09-30T06:33:00Z")

</div>

I have a target `baz` that links against library `foo` in the toolchain sysroot. Now I want to install the `foo` library to `CMAKE_INSTALL_PREFIX`.

---

<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:** [October 1, 2021, 11:35am UTC](https://discourse.cmake.org/t/find-library-so-and-all-symlinks/4160/7 "2021-10-01T11:35:27Z")

</div>

You can use [`file(GET_RUNTIME_DEPENDENCIES)`](https://cmake.org/cmake/help/latest/command/file.html#get-runtime-dependencies) for that.
