QMake - define variable, check value inside project file



  • In my program I want to alter some values in order to make my program compilable for different Desktop Environments. While I can check from pre-set values whether I am running under windows or linux, I am unable to determine the current DE that I am running so as to alter my code and adapt it to each DE every time.

    So, I decided to declare my own variables inside the configuration file.

    @DEFINES += "FOR_GNOME=0"
    DEFINES += "FOR_LUNA=1"
    DEFINES += "FOR_AERO=0"@

    Now I need to check whether a value from the above is true or not. I could just define a variable (e.g. FOR_GNOME) and then use defined() function and #ifdef inside the code in order to determine the target Desktop Environment, but, because of the fact that there are many DEs as targets, I don't want to have to memorize all these variable names (so as to remember what to place inside the .pro file at the "DEFINES +=" each time). So, I want to define all of them and give value 1 to the target DE.

    Now, I need to make preprocessor checks for each DE separately. For example, if I am under GNOME I need specific libraries to link via PKGCONFIG. So , this is what I imagine it to be:

    @DEFINES += "FOR_GNOME=0"
    DEFINES += "FOR_LUNA=1"
    DEFINES += "FOR_AERO=0"

    contains(FOR_GNOME, 1){
    #build for GNOME, link gio-2.0 so as to use the gsettings library
    CONFIG += link_pkgconfig
    PKGCONFIG += gio-2.0
    }

    contains(FOR_LUNA, 1){
    #do something else
    }@
    This is just example code of how I imagine it to be.

    Then, I want to use these inside my code:

    @#if FOR_GNOME
    //code
    #elif FOR_LUNA
    //more code
    ...
    #endif@

    The main problem is that contains() function does not work (while it should, as far as I correctly understand the documentation). Also, I tried:

    @FOR_GNOME {
    #build for GNOME, link gio-2.0 so as to use the gsettings library
    CONFIG += link_pkgconfig
    PKGCONFIG += gio-2.0
    }@

    but it didn't work either. With the 2nd way FOR_GNOME should be inside CONFIG, but then I don't know how to make it available in my code.


  • Lifetime Qt Champion

    Hi,

    You could use something like this:

    @
    CONFIG += gnome
    contains(CONFIG, gnome) {
    DEFINES += FOR_GNOME
    //etc...
    }
    @

    Hope it helps



  • Thanks for the answer. Yes, this seems to be what I am looking for :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.