# Why is my library consumer getting a PRIVATE library dependency

**URL:** https://discourse.cmake.org/t/why-is-my-library-consumer-getting-a-private-library-dependency/5776
**Category:** Usage
**Created:** [June 1, 2022, 6:50pm UTC](https://discourse.cmake.org/t/why-is-my-library-consumer-getting-a-private-library-dependency/5776 "2022-06-01T18:50:15Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![JRR](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@JRR](https://discourse.cmake.org/u/JRR)
#### Post date: [June 1, 2022, 6:50pm UTC](https://discourse.cmake.org/t/why-is-my-library-consumer-getting-a-private-library-dependency/5776/1 "2022-06-01T18:50:15Z")

</div>

I have three build elements:

1. bar (library) provides bar()
2. foo (library) provides foo() and uses bar()
3. main (exec) uses foo()

I purposely have `bar` as a PRIVATE library of `foo`, and expect for `main` to fail because it doesn’t get the link information for `bar`, but it does. Why is a PRIVATE library being shared with the consumer?

Here is my simple CMakeLists.txt file.

```auto
cmake_minimum_required (VERSION 3.18)

project(Example)

add_library(bar STATIC bar/bar.c bar/bar.h)
target_include_directories(bar PUBLIC "${CMAKE_CURRENT_LIST_DIR}/bar")

add_library(foo STATIC foo/foo.c foo/foo.h)
target_include_directories(foo PUBLIC "${CMAKE_CURRENT_LIST_DIR}/foo")
target_link_libraries(foo PRIVATE bar)

add_executable(main main/main.c)
target_link_libraries(main PRIVATE foo)

```

The link line I see for this is

```auto
[100%] Linking C executable main
/usr/local/bin/cmake -E cmake_link_script CMakeFiles/main.dir/link.txt --verbose=1
/usr/bin/cc CMakeFiles/main.dir/main/main.c.o -o main libfoo.a libbar.a 

```

You may ask, why would you make something that would purposely fail. I did this trying to investigate this problem. We have a scenario where the `bar` library has information in it’s header files that `foo` wants to use, but it doesn’t actually use any symbols from `bar`.

In order for `foo` to compile it needs a few header files from `bar` but it doesn’t need `bar.a`. But when we use this mechanism the consumer of `foo` now tries to link with `bar.a` too.

---

<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: [June 1, 2022, 7:43pm UTC](https://discourse.cmake.org/t/why-is-my-library-consumer-getting-a-private-library-dependency/5776/2 "2022-06-01T19:43:58Z")

</div>

> [@JRR](#):
>
> You may ask, why would you make something that would purposely fail. I did this trying to investigate this problem. We have a scenario where the `bar` library has information in it’s header files that `foo` wants to use, but it doesn’t actually use any symbols from `bar`.

Static libraries will forward their private dependency _link_ requirements to consumers to ensure that all symbols are provided.

> [@JRR](#):
>
> In order for `foo` to compile it needs a few header files from `bar` but it doesn’t need `bar.a`. But when we use this mechanism the consumer of `foo` now tries to link with `bar.a` too.

You’re interested in `$<COMPILE_ONLY>` which hides the target from the linker usage requirements. It’s been discussed on Discouse a few times but search isn’t finding it. It’s just a proposal at this point.

---

<div class="post-metadata">

### Author: ![JRR](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/ecccb3/32.png) [@JRR](https://discourse.cmake.org/u/JRR)
#### Post date: [June 1, 2022, 7:54pm UTC](https://discourse.cmake.org/t/why-is-my-library-consumer-getting-a-private-library-dependency/5776/3 "2022-06-01T19:54:33Z")

</div>

Thanks @ben.boeckel. I found the entries doing a google search

```auto
"compile_only" inurl:discourse.cmake.org

```

The discussion for [Add only library headers during target\_link\_libraries()](https://discourse.cmake.org/t/add-only-library-headers-during-target-link-libraries/2973) from 3/2021 is pretty much exactly what I’m wanting.

I also see a workaround in [Idiomatic way to deal with huge number of include directories](https://discourse.cmake.org/t/idiomatic-way-to-deal-with-huge-number-of-include-directories/3947), although it means creating two libraries for this, still it’s a step forwards.

Thank you!
