Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check CMake's option in C++ source

Tags:

c++

g++

cmake

I have the following option defined in CMake:

option(OURAPP-DEV-USE_EXTREME_DEBUGGING "Use extreme debugging features" OFF)

and I would like to check in a C++ file that this option was checked (in the CMake-GUI) or not.

I.e. writing C++ code like:

#if OURAPP-DEV-USE_EXTREME_DEBUGGING
 print_extra_debugging();
#endif

Please note, that our project setup requires that there is a - between the options regarding the components (such as OURAPP and DEV and the rest ...)

Any idea how to make it happen?

like image 610
Ferenc Deak Avatar asked Aug 31 '25 04:08

Ferenc Deak


1 Answers

Transfer the CMake option to the C++ world using a preprocessor define.

IF(OURAPP-DEV-USE_EXTREME_DEBUGGING)
    ADD_DEFINITIONS(-DUSE_EXTREME_DEBUGGING)
ENDIF()

Under the hood, this adds the define to the compiler command line, and is then available to the preprocessor:

#ifdef USE_EXTREME_DEBUGGING
    print_extra_debugging();
#endif

Note that a hyphen is not a valid character in a C preprocessor token, so you'll have to change the name in the define.

like image 200
Peter Avatar answered Sep 02 '25 17:09

Peter