# undefined variable is defined

**URL:** <https://discourse.cmake.org/t/undefined-variable-is-defined/12666>\
**Category:** Code\
**Created:** [October 1, 2024, 7:56pm UTC](https://discourse.cmake.org/t/undefined-variable-is-defined/12666 "2024-10-01T19:56:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![gbburkhardt](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/g/90ced4/32.png) [@gbburkhardt](https://discourse.cmake.org/u/gbburkhardt)\
**Post date:** [October 1, 2024, 7:56pm UTC](https://discourse.cmake.org/t/undefined-variable-is-defined/12666/1 "2024-10-01T19:56:32Z")

</div>

cmake version 3.28.1  
MinGW: MSYS\_NT-10.0-22631 DESKTOP-UH4OPKD 3.4.10.x86\_64 2023-12-22 10:06 UTC x86\_64 Msys

I’ve been debugging a problem related to CheckSymbolExists macro. It seems that the ‘try\_compile’ test is never run. The test  
“if(NOT DEFINED “${VARIABLE}” OR “x${${VARIABLE}}” STREQUAL “x${VARIABLE}”)” failed.

I inserted this code in “macro(\_\_CHECK\_SYMBOL\_EXISTS\_IMPL SOURCEFILE SYMBOL FILES VARIABLE)”

unset(\_MBEDTLS\_V2\_OR\_NEWER)  
if (DEFINED \_MBEDTLS\_V2\_OR\_NEWER)  
message(“\_MBEDTLS\_V2\_OR\_NEWER defined”)  
endif()

and it prints. I am puzzled.

The symbol being checked for is defined in the header and library. I believe that the clause "NOT DEFINED “${VARIABLE}” should have triggered. As far as I can tell, the variable being used was never set, so it should be undefined, and the ‘try\_compile’ test run.

---

<div class="post-metadata">

**Author:** ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)\
**Post date:** [October 1, 2024, 10:22pm UTC](https://discourse.cmake.org/t/undefined-variable-is-defined/12666/2 "2024-10-01T22:22:01Z")

</div>

The command `unset(_MBEDTLS_V2_OR_NEWER)` will clear a _non-cache_ variable if one exists, but if there is a _cache_ variable by that name, it will continue to exist after this call. The expression in `if (DEFINED _MBEDTLS_V2_OR_NEWER)` will be true if _either_ a cache or non-cache variable by that name is defined.

---

<div class="post-metadata">

**Author:** ![gbburkhardt](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/g/90ced4/32.png) [@gbburkhardt](https://discourse.cmake.org/u/gbburkhardt)\
**Post date:** [October 1, 2024, 11:07pm UTC](https://discourse.cmake.org/t/undefined-variable-is-defined/12666/3 "2024-10-01T23:07:44Z")

</div>

That in fact is the case. Somehow, the variable must have gotten into the cache. Inserting ‘unset(\_MBEDTLS\_V2\_OR\_NEWER CACHE)’ allows it to work.

Thanks.
