# Creating Shared libraries and an executable

**URL:** <https://discourse.cmake.org/t/creating-shared-libraries-and-an-executable/2846>\
**Category:** Usage\
**Tags:** os:linux\
**Created:** [February 25, 2021, 5:48pm UTC](https://discourse.cmake.org/t/creating-shared-libraries-and-an-executable/2846 "2021-02-25T17:48:39Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![Angew](https://discourse.cmake.org/user_avatar/discourse.cmake.org/angew/32/229_2.png) [@Angew](https://discourse.cmake.org/u/Angew)\
**Post date:** [February 26, 2021, 9:51am UTC](https://discourse.cmake.org/t/creating-shared-libraries-and-an-executable/2846/6 "2021-02-26T09:51:56Z")

</div>

> [@Q123](#):
>
> `target_sources(mylibA PUBLIC Path/ToLibA/Source )`

Using `PUBLIC` here means “make these sources part of `mylibA` _and also_ make them part of whoever links to `mylibA`”. That is how the sources are getting into your executable. You should probably be using `PRIVATE` here. Actually, I don’t see a reason for you to use `target_sources()` at all: just list the source files directly in the `add_library()` command, and they will be `PRIVATE` by default.

It’s extremely rare to need to use anything other than `PRIVATE` with `target_sources()`. I [answered a similar question](https://discourse.cmake.org/t/ides-and-header-files/2665/2) recently, see that for more info.

---

_[View the full topic](https://discourse.cmake.org/t/creating-shared-libraries-and-an-executable/2846)._
