# check\_symbol\_exists() and #define

**URL:** <https://discourse.cmake.org/t/check-symbol-exists-and-define/13820>\
**Category:** Code\
**Created:** [March 26, 2025, 8:08am UTC](https://discourse.cmake.org/t/check-symbol-exists-and-define/13820 "2025-03-26T08:08:45Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![pol2095](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/e99b99/32.png) [@pol2095](https://discourse.cmake.org/u/pol2095)\
**Post date:** [March 26, 2025, 8:08am UTC](https://discourse.cmake.org/t/check-symbol-exists-and-define/13820/1 "2025-03-26T08:08:45Z")

</div>

Hello,

I try to detect a `#define` in a header file :

```auto
#define USAGE_QUALITY 0u

```

using

```auto
list(APPEND CMAKE_REQUIRED_INCLUDES ${TEST_INCLUDE_DIR})
check_symbol_exists(USAGE_QUALITY test/test.h usage_flag_exists)

```

but `usage_flag_exists` return `false` ?

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:** [March 26, 2025, 9:09am UTC](https://discourse.cmake.org/t/check-symbol-exists-and-define/13820/2 "2025-03-26T09:09:51Z")

</div>

Are you setting up the include directories correctly? Since you’re specifying the file to be included as `test/test.h`, it means that `${TEST_INCLUDE_DIR}` must be a directory which contains directory `test` (which then contains `test.h`). Is that the case?

---

<div class="post-metadata">

**Author:** ![pol2095](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/e99b99/32.png) [@pol2095](https://discourse.cmake.org/u/pol2095)\
**Post date:** [March 26, 2025, 10:17am UTC](https://discourse.cmake.org/t/check-symbol-exists-and-define/13820/3 "2025-03-26T10:17:03Z")

</div>

Yes, it’s that case.

---

<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:** [March 26, 2025, 10:34am UTC](https://discourse.cmake.org/t/check-symbol-exists-and-define/13820/4 "2025-03-26T10:34:36Z")

</div>

Also note that the result of the check is cached, i.e. the symbol existence is only checked in the first CMake run. If you want to force re-evaluation, delete the `usage_flag_exists` cache variable (e.g. by passing `-Uusage_flag_exists` on CMake command line).

---

<div class="post-metadata">

**Author:** ![pol2095](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/e99b99/32.png) [@pol2095](https://discourse.cmake.org/u/pol2095)\
**Post date:** [March 26, 2025, 11:12am UTC](https://discourse.cmake.org/t/check-symbol-exists-and-define/13820/5 "2025-03-26T11:12:10Z")

</div>

Is-it possible using the CMake GUI to delete cache variable ?

---

<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:** [March 26, 2025, 11:57am UTC](https://discourse.cmake.org/t/check-symbol-exists-and-define/13820/6 "2025-03-26T11:57:09Z")

</div>

Yes. Select the variable and press “Remove Entry” button.

---

<div class="post-metadata">

**Author:** ![pol2095](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/e99b99/32.png) [@pol2095](https://discourse.cmake.org/u/pol2095)\
**Post date:** [March 26, 2025, 1:32pm UTC](https://discourse.cmake.org/t/check-symbol-exists-and-define/13820/7 "2025-03-26T13:32:46Z")

</div>

I don’t see cache variable `usage_flag_exists` in CMake GUI ?

---

<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:** [March 26, 2025, 1:37pm UTC](https://discourse.cmake.org/t/check-symbol-exists-and-define/13820/8 "2025-03-26T13:37:08Z")

</div>

You’re right, apparently the module sets the variable as `INTERNAL`. So you will have to run CMake with `-U` from the command line, or manually delete the variable from the `CMakeCache.txt`.

---

<div class="post-metadata">

**Author:** ![pol2095](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/e99b99/32.png) [@pol2095](https://discourse.cmake.org/u/pol2095)\
**Post date:** [March 26, 2025, 1:49pm UTC](https://discourse.cmake.org/t/check-symbol-exists-and-define/13820/9 "2025-03-26T13:49:28Z")

</div>

I added to the beginning of the project CMakeLists.txt file :

```auto
unset(usage_flag_exists CACHE)

```

is-it right ?

---

<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:** [March 26, 2025, 1:54pm UTC](https://discourse.cmake.org/t/check-symbol-exists-and-define/13820/10 "2025-03-26T13:54:16Z")

</div>

Yes, this will remove the cache variable at the start of each CMake run. Which means that the compilation check for the existence of `USAGE_QUALITY` will be performed during every CMake run too. It is up to you to decide if that is what you want; note that it visibly slows down the configure step.

---

<div class="post-metadata">

**Author:** ![pol2095](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/e99b99/32.png) [@pol2095](https://discourse.cmake.org/u/pol2095)\
**Post date:** [March 26, 2025, 2:00pm UTC](https://discourse.cmake.org/t/check-symbol-exists-and-define/13820/11 "2025-03-26T14:00:36Z")

</div>

Deleting internal variable in CMakeCache.txt doesn’t work…

---

<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:** [March 26, 2025, 2:03pm UTC](https://discourse.cmake.org/t/check-symbol-exists-and-define/13820/12 "2025-03-26T14:03:14Z")

</div>

It worked for me. Is there something else which might be interfering with your setup?

---

<div class="post-metadata">

**Author:** ![pol2095](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/e99b99/32.png) [@pol2095](https://discourse.cmake.org/u/pol2095)\
**Post date:** [March 26, 2025, 2:37pm UTC](https://discourse.cmake.org/t/check-symbol-exists-and-define/13820/13 "2025-03-26T14:37:31Z")

</div>

I remove in CMakeCache.txt

> usage\_flag\_exists:INTERNAL=

it’s recreated when I click on `Configure`, without being updated

---

<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:** [March 26, 2025, 2:55pm UTC](https://discourse.cmake.org/t/check-symbol-exists-and-define/13820/14 "2025-03-26T14:55:55Z")

</div>

It’s not really “without being updated,” it’s just that the check fails to find the symbol. You can try running CMake with `--debug-trycompile` to force it to leave the buildsystem generated by the last `try_compile()` on disk. So, the steps would be:

1. Ensure that `check_symbol_exists(USAGE_QUALITY ...)` is the only symbol check, `try_compile()`, or `try_run()` command which will run in your configure step (e.g. comment out all others, if any, or ensure they have their result already cached).
2. Run `cmake -Uusage_flag_exists --debug-trycompile .` in your top-level binary directory.
3. Look into `CMakeFiles/CMakeScratch/TryCompile-*` for the buildsystem CMake used to check the symbol’s existence, and manually inspect why it’s failing to find the symbol.

---

<div class="post-metadata">

**Author:** ![pol2095](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/p/e99b99/32.png) [@pol2095](https://discourse.cmake.org/u/pol2095)\
**Post date:** [March 26, 2025, 2:59pm UTC](https://discourse.cmake.org/t/check-symbol-exists-and-define/13820/15 "2025-03-26T14:59:55Z")

</div>

I also need to close the CMake GUI app.  
Thanks for your help.
