# Tracked down difficult bug: caused by try\_compile() using -D${\_var}

**URL:** https://discourse.cmake.org/t/tracked-down-difficult-bug-caused-by-try-compile-using-d-var/4736
**Category:** Development
**Created:** [December 25, 2021, 9:24pm UTC](https://discourse.cmake.org/t/tracked-down-difficult-bug-caused-by-try-compile-using-d-var/4736 "2021-12-25T21:24:51Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![walki](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/w/cdc98d/32.png) [@walki](https://discourse.cmake.org/u/walki)
#### Post date: [December 25, 2021, 9:24pm UTC](https://discourse.cmake.org/t/tracked-down-difficult-bug-caused-by-try-compile-using-d-var/4736/1 "2021-12-25T21:24:51Z")

</div>

Hi,

In my primecount project I added the following test to check if \_\_int128\_t is supported by the C++ compiler and the C++ standard library:

```auto
check_cxx_source_compiles("
    #include <limits>
    #include <type_traits>
    int main() {
        static_assert(std::numeric_limits<__uint128_t>::max() != 0, \"\");
        static_assert(std::is_integral<__int128_t>::value, \"\");
        static_assert(std::is_integral<__uint128_t>::value, \"\");
        static_assert(std::is_signed<__int128_t>::value, \"\");
        static_assert(std::is_unsigned<__uint128_t>::value, \"\");
        return 0;
    }" int128_t)

```

That test did not work reliably. Somehow I could fix the issues I encountered by changing the output variable name to int128 instead of int128\_t. This is really scary to me, why does the test result depend on the output variable name?! So I read the CMake source code and found that check\_cxx\_source\_compiles() uses try\_compile() internally and there I found ([CMake/CheckSourceCompiles.cmake at 1d225c5c0ca86592e835b626c55aa6bf4faa2f3d · Kitware/CMake · GitHub](https://github.com/Kitware/CMake/blob/1d225c5c0ca86592e835b626c55aa6bf4faa2f3d/Modules/Internal/CheckSourceCompiles.cmake#L95)):

```auto
function(CMAKE_CHECK_SOURCE_COMPILES _lang _source _var)
    ...
    try_compile(${_var}
      COMPILE_DEFINITIONS -D${_var} ${CMAKE_REQUIRED_DEFINITIONS}

```

Why is -D${\_var} needed? I think this causes the bug I encountered. It seems to me that the -D${\_var} definition makes it impossible to e.g. use an output variable name in check\_cxx\_source\_compiles() that corresponds to any C/C++ keyword! There are loads of C/C++ keywords and new one are introduced from time to time. Even worse than that if the output variable name is used e.g. as a variable or function name inside the C/C++ test code or any of its headers the test will also fail!

---

<div class="post-metadata">

### Author: ![walki](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/w/cdc98d/32.png) [@walki](https://discourse.cmake.org/u/walki)
#### Post date: [December 26, 2021, 8:58am UTC](https://discourse.cmake.org/t/tracked-down-difficult-bug-caused-by-try-compile-using-d-var/4736/2 "2021-12-26T08:58:11Z")

</div>

I have convinced myself that this is a CMake bug, therefore I have reposted this issue on the CMake Gitlab issue tracker: [https://gitlab.kitware.com/cmake/cmake/-/issues/23050](https://gitlab.kitware.com/cmake/cmake/-/issues/23050)
