# Testing if a compiler feature has been required on a target

**URL:** https://discourse.cmake.org/t/testing-if-a-compiler-feature-has-been-required-on-a-target/6931
**Category:** Usage
**Created:** [November 22, 2022, 9:16pm UTC](https://discourse.cmake.org/t/testing-if-a-compiler-feature-has-been-required-on-a-target/6931 "2022-11-22T21:16:56Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![David\_Hunter](https://discourse.cmake.org/user_avatar/discourse.cmake.org/david_hunter/32/115_2.png) [@David\_Hunter](https://discourse.cmake.org/u/David_Hunter)
#### Post date: [November 22, 2022, 9:16pm UTC](https://discourse.cmake.org/t/testing-if-a-compiler-feature-has-been-required-on-a-target/6931/1 "2022-11-22T21:16:56Z")

</div>

If I require a compiler feature like the example in the documentation

```auto
target_compile_features(mylib PUBLIC cxx_constexpr)

```

How do I then test that the target has had that feature required So I want some code that does  
`if target mylib requires cxx_constexpr`

My use case is that in a function I want to check if the target passed in has required C++ cxx\_std\_20

Note I tired the target propertis CXX\_STANDARD and CXX\_STANDARD\_REQUIRED but they don’t seem to be set

---

<div class="post-metadata">

### Author: ![thehans](https://discourse.cmake.org/user_avatar/discourse.cmake.org/thehans/32/2134_2.png) [@thehans](https://discourse.cmake.org/u/thehans)
#### Post date: [November 23, 2022, 1:19am UTC](https://discourse.cmake.org/t/testing-if-a-compiler-feature-has-been-required-on-a-target/6931/2 "2022-11-23T01:19:17Z")

</div>

It looks like you need to `get_target_property` for `COMPILE_FEATURES`

[https://cmake.org/cmake/help/latest/command/target\_compile\_features.html](https://cmake.org/cmake/help/latest/command/target_compile_features.html)

> `PRIVATE` and `PUBLIC` items will populate the [`COMPILE_FEATURES`](https://cmake.org/cmake/help/latest/prop_tgt/COMPILE_FEATURES.html#prop_tgt:COMPILE_FEATURES) property of `<target>` . `PUBLIC` and `INTERFACE` items will populate the [`INTERFACE_COMPILE_FEATURES`](https://cmake.org/cmake/help/latest/prop_tgt/INTERFACE_COMPILE_FEATURES.html#prop_tgt:INTERFACE_COMPILE_FEATURES) property of `<target>` .

---

<div class="post-metadata">

### Author: ![David\_Hunter](https://discourse.cmake.org/user_avatar/discourse.cmake.org/david_hunter/32/115_2.png) [@David\_Hunter](https://discourse.cmake.org/u/David_Hunter)
#### Post date: [November 23, 2022, 3:50am UTC](https://discourse.cmake.org/t/testing-if-a-compiler-feature-has-been-required-on-a-target/6931/3 "2022-11-23T03:50:12Z")

</div>

> [@thehans](#):
>
> [`COMPILE_FEATURES`](https://cmake.org/cmake/help/latest/prop_tgt/COMPILE_FEATURES.html#prop_tgt:COMPILE_FEATURES)

I had seen that and tried it and thought it didn’t work. My problem is that it doesn’t seem to work transitively. If I have a target foo and do

`target_compile_features(foo PUBLIC cxx_std_23)`

then the COMPILE\_FEATURES contains cxx\_std\_23 as expected.  
However if I then have a target bar with the following

`target_link_libraries(bar PUBLIC foo)`

Then for bar COMPILE\_FEATURES come back as not found. The generated project does seem to set the language version of bar to C++23. So this seems to be working as intended. So how can I find out the full set of COMPILE\_FEATURES both direct and indirect, do I have to walk the dependency tree myself?

---

<div class="post-metadata">

### Author: ![thehans](https://discourse.cmake.org/user_avatar/discourse.cmake.org/thehans/32/2134_2.png) [@thehans](https://discourse.cmake.org/u/thehans)
#### Post date: [November 23, 2022, 6:18pm UTC](https://discourse.cmake.org/t/testing-if-a-compiler-feature-has-been-required-on-a-target/6931/4 "2022-11-23T18:18:55Z")

</div>

Ah, I didn’t know you were trying to read the properties on a _dependent_ target.

You could also try setting `CXX_STANDARD` directly.  
My project uses settings like this for example:

```auto
set_property(TARGET foo PROPERTY CXX_STANDARD 17)
set_property(TARGET foo PROPERTY CXX_EXTENSIONS OFF)
set_property(TARGET foo PROPERTY CXX_STANDARD_REQUIRED ON)

```

As I understand it, properties like that _won’t_ be inherited by dependent targets…  
But I think that [INTERFACE libraries](https://cmake.org/cmake/help/v3.7/command/add_library.html#interface-libraries) exist to address that issue.  
I haven’t really done much configuring libraries in cmake though (mostly just building executables), so I could be wrong.
