c++qtqmake

Defining a bool in Qt pro file


Setting a boolean value for a variable in .pro file can be done the following way:

//ProjectFile.pro
DEFINES += "myBool=1"

This variable can be used in source code *.cpp for conditional compiling. Qt even highlights the conditional expression: enter image description here



Now Iam looking for a way to use that myBool variable in .pro file

According to Qt documentary conditional statements work like this: https://doc.qt.io/archives/qt-4.8/qmake-advanced-usage.html

<condition> {
    <command or definition>
    ...
}

Outgoing from my very basic qmake knowledge the following approach didnt work

//ProjectFile.pro
CONFIG(debug, debug|release) {message("Hello from DebugMode")} // => works fine

$$myBool {message("Hello from myBool")} // => not working

Question: Does somebody know how to handle conditional statements in pro files with a defined variable like myBool?


Solution

  • Out of curiosity, I researched a bit…

    First, I consulted qmake Manual > Advanced Usage but that didn't help much.

    Then I tried to find something with google and found the following two Q/As (among others):

    I combined what I've found and tested it in my qmake (on Debian):

    CONFIG+=MY_BOOL
    
    MY_BOOL {
      DEFINES+="MY_BOOL=1"
    }
    
    message(Defines: $$DEFINES)
    
    MY_BOOL {
      message(MY_BOOL defined)
    }
    !MY_BOOL {
      message(MY_BOOL not defined)
    }
    

    Output:

    Project MESSAGE: Defines: MY_BOOL=1
    Project MESSAGE: MY_BOOL defined
    

    To counter check, I changed the first line:

    #CONFIG+=MY_BOOL
    

    Output:

    Defines: 
    Project MESSAGE: MY_BOOL not defined