# Some built-in helpers to convert OFF/ON in variable into the 0/1

**URL:** https://discourse.cmake.org/t/some-built-in-helpers-to-convert-off-on-in-variable-into-the-0-1/3958
**Category:** Development
**Created:** [August 19, 2021, 5:57pm UTC](https://discourse.cmake.org/t/some-built-in-helpers-to-convert-off-on-in-variable-into-the-0-1/3958 "2021-08-19T17:57:36Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![leha-bot](https://discourse.cmake.org/user_avatar/discourse.cmake.org/leha-bot/32/921_2.png) [@leha-bot](https://discourse.cmake.org/u/leha-bot)
#### Post date: [August 19, 2021, 5:57pm UTC](https://discourse.cmake.org/t/some-built-in-helpers-to-convert-off-on-in-variable-into-the-0-1/3958/1 "2021-08-19T17:57:36Z")

</div>

Some function like to\_01( [SRC=]) like `#cmakedefine01`.  
It could be useful in some contexts , e.g. consider some header-only library with its `option()` for enabling diagnostics which would set some internal `#define` with another name from another library which we, as library author, don’t control:

```auto
option(PROJ_ENABLE_DIAGNOSTICS "Enable diagnostics" ON)
...
add_library(proj_api INTERFACE)
to_01(var PROJ_ENABLE_DIAGNOSTICS) # var now has 0/1 depending on OFF/ON (like in `if()` or like cmakedefine01 but value only
target_compile_definitions(proj_api INTERFACE ENABLE_DIAGNOSTICS=${var}) # the proj_api library now provides new compile definition ENABLE_DIAGNOSTICS=0/1

```

(it will be easier to make as a function returning value, but it’s currently impossible in CMake language)

Thanks.

---

<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: [August 19, 2021, 7:27pm UTC](https://discourse.cmake.org/t/some-built-in-helpers-to-convert-off-on-in-variable-into-the-0-1/3958/3 "2021-08-19T19:27:11Z")

</div>

> [@leha-bot](#):
>
> `target_compile_definitions(proj_api INTERFACE ENABLE_DIAGNOSTICS=${var})`

Even better:

```cmake
target_compile_definitions(proj_api INTERFACE
  "ENABLE_DIAGNOSTICS=$<BOOL:${PROJ_ENABLE_DIAGNOSTICS}>")

```
