# Consecutive target\_link\_libraries with and without PUBLIC

**URL:** https://discourse.cmake.org/t/consecutive-target-link-libraries-with-and-without-public/3592
**Category:** Code
**Created:** [June 18, 2021, 2:23pm UTC](https://discourse.cmake.org/t/consecutive-target-link-libraries-with-and-without-public/3592 "2021-06-18T14:23:38Z")
**Posts on this page:** 6
**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: [June 18, 2021, 2:23pm UTC](https://discourse.cmake.org/t/consecutive-target-link-libraries-with-and-without-public/3592/1 "2021-06-18T14:23:38Z")

</div>

cmake requires consecutive target\_link\_libraries invocations to use same syntax. E.g. the following will not work;

target\_link\_libraries(target PUBLIC A)  
target\_link\_libraries(target B)

Any way to get around that?

---

<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: [June 18, 2021, 2:29pm UTC](https://discourse.cmake.org/t/consecutive-target-link-libraries-with-and-without-public/3592/2 "2021-06-18T14:29:11Z")

</div>

```auto
target_link_libraries(target PUBLIC A)
target_link_libraries(target PRIVATE B)

```

---

<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: [June 18, 2021, 3:02pm UTC](https://discourse.cmake.org/t/consecutive-target-link-libraries-with-and-without-public/3592/3 "2021-06-18T15:02:12Z")

</div>

You may not know that whether PUBLIC/PRIVATE was used, they may be called from different contexts.

Consider the following;

```auto
function(my_add_library library)
   add_library(${library} ${ARGN})
   target_link_libraries(${library} PRIVATE C)
endfunction()

my_add_library(A ...)
target_link_libraries(A B)

```

---

<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: [June 18, 2021, 3:05pm UTC](https://discourse.cmake.org/t/consecutive-target-link-libraries-with-and-without-public/3592/4 "2021-06-18T15:05:29Z")

</div>

But I assume you always know how you want to link B, right? Or are you in a situation where you’re trying to express “link B into `target` with the same visibility with which A was linked into it”? That would be a very unusual requirement, I’d say.

Note that mixing visibilities is perfectly fine, even in the same command:

```auto
target_link_libraries(target PUBLIC A PRIVATE B C PUBLIC D)
```

---

<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: [August 9, 2021, 8:34am UTC](https://discourse.cmake.org/t/consecutive-target-link-libraries-with-and-without-public/3592/6 "2021-08-09T08:34:31Z")

</div>

`target_link_libraries` must have a default if `PUBLIC/PRIVATE` is omitted. Still the following fails.

```auto
target_link_libraries(target PUBLIC A)
target_link_libraries(target B)

```

Does not make sense.

---

<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: [September 3, 2021, 9:23am UTC](https://discourse.cmake.org/t/consecutive-target-link-libraries-with-and-without-public/3592/7 "2021-09-03T09:23:03Z")

</div>

Yes, it has such a default, for when `PUBLIC`/`PRIVATE` is omitted _always_. However, once you use such a keyword in one invocation, you have to use it in all of them.

The way to think about it is to consider `target_link_libraries()` to have two distinct modes of operation: plain one, and keyword-based one. You can use it just fine in either mode, but you cannot mix modes for the same consuming target.
