How can I define a C++ preprocessor macro through the command line with CMake?

A good alternative would be to define a CMake option:

OPTION(DEFINE_MACRO "Option description" ON) # Enabled by default

Followed by a condition:

IF(DEFINE_MACRO)
    ADD_DEFINITIONS(-DMACRO)
ENDIF(DEFINE_MACRO)

Then you can turn that option ON/OFF via the command line with CMake using the -D flag. Example:

cmake -DDEFINE_MACRO=OFF ..

To make sure the compiler is receiving the definition right, you can call make in verbose mode and check for the macro being defined or not:

make VERBOSE=1

This is a good solution also because make will recompile your code when any of CMake options changes.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)