# How does target\_link\_libraries with the PUBLIC flag actually work?

**URL:** https://discourse.cmake.org/t/how-does-target-link-libraries-with-the-public-flag-actually-work/4268
**Category:** Usage
**Tags:** os:linux
**Created:** [October 13, 2021, 3:08pm UTC](https://discourse.cmake.org/t/how-does-target-link-libraries-with-the-public-flag-actually-work/4268 "2021-10-13T15:08:23Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Dylan\_Reedy](https://discourse.cmake.org/user_avatar/discourse.cmake.org/dylan_reedy/32/1533_2.png) [@Dylan\_Reedy](https://discourse.cmake.org/u/Dylan_Reedy)
#### Post date: [October 13, 2021, 3:08pm UTC](https://discourse.cmake.org/t/how-does-target-link-libraries-with-the-public-flag-actually-work/4268/1 "2021-10-13T15:08:23Z")

</div>

I’m curious how the use of the PUBLIC flag in target\_link\_libraries actually works. As an example, say I create a library libA.so which depends on libB.so. Some elements of libB.so are in the public interface of A, so I configure it with cmake thusly:

target\_link\_library(A PUBLIC B)

Now, someone creates a library libUserLibrary.so and wants to use my library A. They link against it, and thereby libUserLibrary.so links against both libA.so and libB.so.

How is this actually done with the C++ linker?

Thanks!

---

<div class="post-metadata">

### Author: ![robert.maynard](https://discourse.cmake.org/user_avatar/discourse.cmake.org/robert.maynard/32/4_2.png) [@robert.maynard](https://discourse.cmake.org/u/robert.maynard)
#### Post date: [October 14, 2021, 2:28pm UTC](https://discourse.cmake.org/t/how-does-target-link-libraries-with-the-public-flag-actually-work/4268/2 "2021-10-14T14:28:19Z")

</div>

> [@Dylan\_Reedy](#):
>
> Now, someone creates a library libUserLibrary.so and wants to use my library A. They link against it, and thereby libUserLibrary.so links against both libA.so and libB.so.
> 
> How is this actually done with the C++ linker?

CMake creates a link line for libUserLibrary.so that has both libA.so and libB.so on it ( `g++ -fPIC -shared ... -o libUserLibrary.so <path/>libA.so <path/>libB.so ...` )

---

<div class="post-metadata">

### Author: ![Dylan\_Reedy](https://discourse.cmake.org/user_avatar/discourse.cmake.org/dylan_reedy/32/1533_2.png) [@Dylan\_Reedy](https://discourse.cmake.org/u/Dylan_Reedy)
#### Post date: [October 15, 2021, 12:01am UTC](https://discourse.cmake.org/t/how-does-target-link-libraries-with-the-public-flag-actually-work/4268/3 "2021-10-15T00:01:19Z")

</div>

How does the linker become aware that libB is “public”, though?

---

<div class="post-metadata">

### Author: ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)
#### Post date: [October 15, 2021, 11:47am UTC](https://discourse.cmake.org/t/how-does-target-link-libraries-with-the-public-flag-actually-work/4268/4 "2021-10-15T11:47:50Z")

</div>

Linker has no idea 😉

CMake tracks those things and in the end generates the linker command. If you used `PUBLIC` or `INTERFACE`, the linker command will look like @robert.maynard showed. If you used `PRIVATE`, it’ll be `g++ -fPIC -shared ... -o libUserLibrary.so <path/>libA.so ...`
