# try\_compile generated temporary targets don't honor link\_libraries.

**URL:** <https://discourse.cmake.org/t/try-compile-generated-temporary-targets-dont-honor-link-libraries/4767>\
**Category:** Code\
**Created:** [January 4, 2022, 3:24am UTC](https://discourse.cmake.org/t/try-compile-generated-temporary-targets-dont-honor-link-libraries/4767 "2022-01-04T03:24:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amos\_Bird](https://discourse.cmake.org/user_avatar/discourse.cmake.org/amos_bird/32/2062_2.png) [@Amos\_Bird](https://discourse.cmake.org/u/Amos_Bird)\
**Post date:** [January 4, 2022, 3:24am UTC](https://discourse.cmake.org/t/try-compile-generated-temporary-targets-dont-honor-link-libraries/4767/1 "2022-01-04T03:24:39Z")

</div>

Hello! My project has a global `link_libraries` call which introduces a different libcxx headers and standard libs. It works fine for all my code and dependencies. However, things like CheckTypeSize and CheckFunctionExists which uses try\_compile don’t work at all, because my toolchain relies on the specified default headers/libs to work correctly.

I also tried setting CMAKE\_CXX\_STANDARD\_LIBRARIES and CMAKE\_C\_STANDARD\_LIBRARIES, but try\_compile still cannot see them. The only way to make it work is using CMAKE\_CXX\_FLAGS which looks dirty.

I also notice a flag: CMAKE\_TRY\_COMPILE\_TARGET\_TYPE. When doing cross-compiling, it defaults to STATIC\_LIBRARY, which can make checks work, but that’s too permissive. check\_library\_exists will almost always return true. So I have to force CMAKE\_TRY\_COMPILE\_TARGET\_TYPE = EXECUTABLE

Is there any standard way of adding default headers/libs to try\_compile?

---

<div class="post-metadata">

**Author:** ![rleigh](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/r/9de053/32.png) [@rleigh](https://discourse.cmake.org/u/rleigh)\
**Post date:** [August 20, 2024, 6:46am UTC](https://discourse.cmake.org/t/try-compile-generated-temporary-targets-dont-honor-link-libraries/4767/2 "2024-08-20T06:46:04Z")

</div>

Did you try `CMAKE_REQUIRED_LIBRARIES`? A common pattern is to save and restore it like this:

```
set(CMAKE_REQUIRED_LIBRARIES_SAVE ${CMAKE_REQUIRED_LIBRARIES})
set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} extra_libs_here)
check_xxx_here()
set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES_SAVE})

```
