I can't get cmake
to test if a preprocessor has been defined or not. Eg:
cmake_minimum_required(VERSION 2.8.9)
project (cmake-test)
add_definitions(-DOS=LINUX)
if(NOT <what condition goes here?>)
message(FATAL_ERROR "OS is not defined")
endif()
The following tests don't work:
if (NOT COMMAND OS)
if (NOT DEFINED OS)
if (NOT OS)
I can get it to work by using set()
and just testing for a regular cmake
variable and then defining the preprocessor macro. Eg:
set(OS LINUX)
if (OS)
add_definitions(-DOS=${OS})
else()
message(FATAL_ERROR "OS is not defined")
endif()
In case, you're wondering why I need to test it if the variable/preprocessor is in the same file, it's because in the final implementation these will come from an external file which is include
ed in the main CMakeFile.txt Eg:
include(project_defs.txt)
if (OS)
....
This is to complete the answer by arrowd.
I also tried the COMPILE_DEFINITIONS option as mentioned above by arrowd unsuccessfully.
Following the documentation of CMake, at least for version 3.x, it turns out that when you call
add_definitions()
in CMake, it adds the definitions to the COMPILE_DEFINITIONS directory property.Therefore, lets say you are defining the following as per your code:
To retrieve the string with the definitions added into the variable "MYDEFS" you can use the following lines in CMake:
Then you can check if in
${MYDEFS}
the define you are looking for exists or not. For instanceNormally, all definitions that are passed to the compiler are controlled by CMake. That is, you create a CMake variable with
or
User sets them via
cmake -D OS=DOS
or in the CMake GUI. Then you can useif()
operator to conditionallyadd_definitions()
to the compiler command line.UPDATE:
If you really want to access preprocessor flags, there is a COMPILE_DEFINITIONS target property. You can access it this way: