how to check a definition exist in CMake

2019-08-17 07:10发布

问题:

I declared a MMACRO in the main CmakeList.txt, using add_definitions( -DMMACRO ). Then when I check at CMakeList in a subfolder, where I use

if(COMMAND MMACRO)
  #something here 
endif()

It is not working. Does anyone know what should I do? Thanks.

how-to-check-if-a-macro-exists-in-cmake

回答1:

copt the answer from the below link cmake: how to check if preprocessor is defined

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:

add_definitions(-DOS=LINUX) To retrieve the string with the definitions added into the variable "MYDEFS" you can use the following lines in CMake:

get_directory_property(MYDEFS COMPILE_DEFINITIONS) MESSAGE( STATUS "Compile defs contain: " ${MYDEFS} ) Then you can check if in ${MYDEFS} the define you are looking for exists or not. For instance

if(MYDEFS MATCHES "^OS=" OR MYDEFS MATCHES ";OS=")
    MESSAGE( STATUS "OS defined" )
else()
    # You can define your OS here if desired
endif()


标签: cmake