# Avoiding duplicate symbols when linking a library multiple times

**URL:** https://discourse.cmake.org/t/avoiding-duplicate-symbols-when-linking-a-library-multiple-times/3816
**Category:** Code
**Created:** [July 22, 2021, 5:51pm UTC](https://discourse.cmake.org/t/avoiding-duplicate-symbols-when-linking-a-library-multiple-times/3816 "2021-07-22T17:51:56Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![jsuthe](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jsuthe/32/1656_2.png) [@jsuthe](https://discourse.cmake.org/u/jsuthe)
#### Post date: [July 22, 2021, 5:51pm UTC](https://discourse.cmake.org/t/avoiding-duplicate-symbols-when-linking-a-library-multiple-times/3816/1 "2021-07-22T17:51:56Z")

</div>

This may be a silly question, but here it goes.

I have a library (call it `myLib`) that depends on `yaml` as well as another third-party library, `libA`, which internally builds a version of `yaml`.

Dependency looks like:

```auto
myLib
 |-- yaml version x
 +-- libA
    +-- yaml version y

```

I’m getting duplicate `yaml` symbols when linking `myLib` into an executable.

Is there any way to avoid this, other than forcing `myLib` to adopt `yaml version y`?

A few other pieces of information: `myLib` is built in CMake, so is `yaml`, but `libA` is built in scons.

Thanks!

---

<div class="post-metadata">

### Author: ![jtxa](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jtxa/32/1535_2.png) [@jtxa](https://discourse.cmake.org/u/jtxa)
#### Post date: [July 22, 2021, 7:37pm UTC](https://discourse.cmake.org/t/avoiding-duplicate-symbols-when-linking-a-library-multiple-times/3816/2 "2021-07-22T19:37:54Z")

</div>

Every symbol with exernal linkage needs to be unique. You cannot have two different versions of a function with the same name in one executable.  
This would only work with header only libraries that do not create any such symbol.

But it should work If you create `libA` as shared library instead.

The other question is if you really want to have two different versions that potentially behave differently in the same executable.

---

<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: [July 23, 2021, 12:52pm UTC](https://discourse.cmake.org/t/avoiding-duplicate-symbols-when-linking-a-library-multiple-times/3816/3 "2021-07-23T12:52:00Z")

</div>

This is a big reason why bundling is bad 🙂 . `libA` could alternatively mangle all of the `libyaml` symbols in its internal copy to avoid such things, but this is so rare and means that `FetchContent` doesn’t generally work (since the project needs patched), so it’s “never” done. If you do port it all to `FetchContent`, there is some mechanism to at least get all of them agreeing to use the same `libyaml`. Though you still have the issue if someone wants to consume `myLib` with their own `libyaml` (which may come in through `otherLib`) as well…
