Debugging cmake

I am compiling a software package and the configuration step of cmake is adding flags to CMAKE_CXX_FLAGS variable that is causing problems when building the software. I would like to know what step in cmake process these flags are being added. This package has a lot of dependencies, so I am wondering if one of the find package modules in cmake is appending these flags.

Is there a cmake debugger that allows me step through the cmake process and examine the values of the variables set? If not, are there other strategies (like cmake options) that can help answer my question?

As of CMake 3.27, there are some interactive debugger options from both Jetbrains CLion and Microsoft’s VScode, though I can’t say I’m familiar with either.

There are also several cmake command line options that might be useful. They provide similar stepwise information as an output.

       --trace
              Put cmake in trace mode.
              Print a trace of all calls made and from where.

       --trace-expand
              Put cmake in trace mode.
              Like --trace, but with variables expanded.

       --trace-source=<file>
              Put cmake in trace mode, but output only lines  of  a  specified
              file.
              Multiple options are allowed.

       --trace-redirect=<file>
              Put  cmake in trace mode and redirect trace output to a file in‐
              stead of stderr.

and there are a few --debug* arguments

   --debug-output
              Put cmake in a debug mode.
              Print extra information during the cmake run like stack traces with message(SEND_ERROR) calls.

and in 3.25 they added:

`--debug-find-var=` option to enable debug messages for `find_*` calls that use specific result variables.```

In addition to jpsessin1’s suggestions, you could also descend into your cmake build directory and examine the outputs.

I’ll use one of my own current issues as an example. I have a cmake build failing due to some compilation error.

I am building in a directory ./build. I went into the “CMakeFiles” directory in build, and examined the output of the file “CMakeConfigureLog.yaml”

Near the end of the output I saw one of the issues encountered that resulted in the failed compilation:

    Linking CXX executable cmTC_20231
    /sw8/cmake-3.26.3-gcc/bin/cmake -E cmake_link_script CMakeFiles/cmTC_20231.dir/link.txt --verbose=1
    /sw8/intel/2024.0/oneapi/compiler/2024.0/bin/icpx  --gcc-toolchain=/usr/  -std=c++17 -lstdc++fs  CMakeFiles/cmTC_20231.dir/src.cxx.o -o cmTC_20231 
    CMakeFiles/cmTC_20231.dir/src.cxx.o: In function `foo()':
    src.cxx:(.text+0xd1): undefined reference to `std::filesystem::current_path[abi:cxx11]()'
    icpx: error: linker command failed with exit code 1 (use -v to see invocation)
    gmake[1]: *** [CMakeFiles/cmTC_20231.dir/build.make:100: cmTC_20231] Error 1

Here we can see that a compilation of a test unit had failed, causing cmake to cancel the rest of the build. From this point, I can attempt to figure out what caused the issue and hopefully resolve the problem.

Additional debug options may preserve the files. In my example, the compiled code was deleted after the failed cmake, so I need to test if including the debug options preserve the files so I can manually run compile commands to isolate what libraries or other options may be missing.

I hope this helps.

1 Like