1

I have following statement in my main CMakefile.txt:

add_definitions( -DAPP_ROLE_IS_QT )

In another CMakefile.txt that sits in a sub-dir, I need to check whether "APP_ROLE_IS_QT" has been defined or not, and do things accordingly:

if( DEFINED APP_ROLE_IS_QT )
    message( STATUS "It exitsts." )
    # do something ...
else()
    message( STATUS "It doesn't!!" )
    # do something else...
endif()

But it does NOT work, I always get "It doesn't!!";

CMAKE VERSION 3.16

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
Leon
  • 1,489
  • 1
  • 12
  • 31
  • So, what exactly do you want: define a **compiler macro** (with `add_definition`) or declare a **CMake variable** which you can check after (with `if(DEFINED)`)? – Tsyvarev Nov 16 '21 at 10:07
  • Sorry, my English is poor. I want define a macro so that I can use it in C++ source, and at same time, I need check whether it exists in cmake source and do right things. – Leon Nov 16 '21 at 10:20
  • Just define a **variable** `APP_ROLE_IS_QT` using `set` command. Using `if` in the main `CMakeLists.txt`, you could **conditionally** execute `add_definitions( -DAPP_ROLE_IS_QT )` in it. Using similar `if` in the sub-dir `CMakefile.txt`, you could conditionally execute `message`. If you want a user of your project to be able to set (or not set) the variable, then see that question: https://stackoverflow.com/questions/5998186/adding-command-line-options-to-cmake – Tsyvarev Nov 16 '21 at 10:36
  • thank you, I have used your solution i.e. option() – Leon Nov 16 '21 at 10:48

0 Answers0