Search code examples
cmake

Does cmake has an option to avoid using undefined variables (like bash set-u)


Does cmake have a mechanism to generate an error when using a undefined variable, a bit like set -u option in bash.

I have a big project composed of several CMakeLists.txt files, representing ~1500 lines, so it is quite difficult to use this construction: if(NOT DEFINED VAR_NAME)

In a ideal world, the following CMakeLists.txt whould fail.

cmake_minimum_required(VERSION 3.13)
message(STATUS "Will delete ${DIR}/${FILE}")
  • Does such option exist? (I don't think so, but the cmake documentation is huge, I may have missed it)
  • Is such behavior on the project roadmap?

Solution

  • With version 3.5.x, the new -Werror=dev combined with --warn-uninitialized has the behavior I wanted.

    So final solution is just to create an alias:

    alias cmake="cmake -Werror=dev --warn-uninitialized"