Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

cmake

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?
like image 500
Mathieu Avatar asked May 24 '26 19:05

Mathieu


1 Answers

As mentioned by KamilCuk, there is the --warn-uninitialized flag which you can pass when invoking CMake to configure a project. If you want it to be an error, then add -Werror=dev.

In a CMake configure preset, you can use the warnings > uninitialized member.

like image 184
starball Avatar answered Jun 04 '26 22:06

starball