# Behavior of target\_link\_libraries when target is a static lib

**URL:** https://discourse.cmake.org/t/behavior-of-target-link-libraries-when-target-is-a-static-lib/10898
**Category:** Usage
**Created:** [May 24, 2024, 8:57am UTC](https://discourse.cmake.org/t/behavior-of-target-link-libraries-when-target-is-a-static-lib/10898 "2024-05-24T08:57:03Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![martingalvan-volue](https://discourse.cmake.org/user_avatar/discourse.cmake.org/martingalvan-volue/32/4483_2.png) [@martingalvan-volue](https://discourse.cmake.org/u/martingalvan-volue)
#### Post date: [May 24, 2024, 8:57am UTC](https://discourse.cmake.org/t/behavior-of-target-link-libraries-when-target-is-a-static-lib/10898/1 "2024-05-24T08:57:03Z")

</div>

Hi, I’m using CMake 3.29.0-rc3 with the Ninja generator on Windows with MSVC. I have the following CMakeLists.txt:

```auto
cmake_minimum_required(VERSION 3.29.0 FATAL_ERROR)

project(test C)

add_library(my_lib STATIC my_lib.c)

target_link_libraries(my_lib PUBLIC something_that_does_not_exist)

```

I see that I’m able to configure and build the project without any errors, even though `something_that_does_not_exist` is set as a dependency of `my_lib`. I presume this will error out at the build stage if I try to link, say, an executable against `my_lib`, but I wonder whether it would be better to have CMake itself emit an error when it realizes that a target depends on something that doesn’t exist.

---

<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: [May 24, 2024, 11:41am UTC](https://discourse.cmake.org/t/behavior-of-target-link-libraries-when-target-is-a-static-lib/10898/2 "2024-05-24T11:41:17Z")

</div>

I don’t think that’s doable. Remember that `target_link_libraries()` also accepts plain library names (not just CMake target names) as arguments, and passes these in a platform-appropriate way to the linker. To signal an error for non-existent things, CMake would have to recreate (each) linker’s search process. Plus it’s possible that the library will be somehow made available (e.g. downloaded) by an earlier build step, so a CMake error is definitely not always appropriate in this case.

---

<div class="post-metadata">

### Author: ![vardarirrgan1](https://discourse.cmake.org/user_avatar/discourse.cmake.org/vardarirrgan1/32/4175_2.png) [@vardarirrgan1](https://discourse.cmake.org/u/vardarirrgan1)
#### Post date: [May 31, 2024, 7:28pm UTC](https://discourse.cmake.org/t/behavior-of-target-link-libraries-when-target-is-a-static-lib/10898/3 "2024-05-31T19:28:47Z")

</div>

It is possible in CMake 3.23 and higher. See [https://cmake.org/cmake/help/latest/prop\_tgt/LINK\_LIBRARIES\_ONLY\_TARGETS.html](https://cmake.org/cmake/help/latest/prop_tgt/LINK_LIBRARIES_ONLY_TARGETS.html)

---

<div class="post-metadata">

### Author: ![martingalvan-volue](https://discourse.cmake.org/user_avatar/discourse.cmake.org/martingalvan-volue/32/4483_2.png) [@martingalvan-volue](https://discourse.cmake.org/u/martingalvan-volue)
#### Post date: [June 3, 2024, 6:34am UTC](https://discourse.cmake.org/t/behavior-of-target-link-libraries-when-target-is-a-static-lib/10898/4 "2024-06-03T06:34:26Z")

</div>

Ah, very interesting. Thanks for the response.

@Angew is there a catch to this that I’m not seeing, or is @vardarirrgan1’s answer correct? Thanks.

---

<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 3, 2024, 4:02pm UTC](https://discourse.cmake.org/t/behavior-of-target-link-libraries-when-target-is-a-static-lib/10898/5 "2024-06-03T16:02:19Z")

</div>

I believe it’s correct. I wasn’t aware of `LINK_LIBRARIES_ONLY_TARGETS`’s existence when I answered.

---

<div class="post-metadata">

### Author: ![vardarirrgan1](https://discourse.cmake.org/user_avatar/discourse.cmake.org/vardarirrgan1/32/4175_2.png) [@vardarirrgan1](https://discourse.cmake.org/u/vardarirrgan1)
#### Post date: [June 3, 2024, 4:28pm UTC](https://discourse.cmake.org/t/behavior-of-target-link-libraries-when-target-is-a-static-lib/10898/6 "2024-06-03T16:28:22Z")

</div>

I suppose the catch is that it is pretty new. 😉
