Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding compiler vendor / version using qmake

Is there any way to get the version and vendor of the compiler used by the user through qmake? What I need is to disable building some targets of my project when g++ 3.x is used and enable them when g++ 4.x is used.

Update: Most answers targeted the preprocessor. This is something that I want to avoid. I don't want a target to be build for a specific compiler version and I want this decision to be made by the build system.

like image 230
Yorgos Pagles Avatar asked Apr 29 '09 07:04

Yorgos Pagles


4 Answers

My answer based on Caleb Huitt - cjhuitt's. But his approach does not work for me.

*-g++ {
    GCC_VERSION = $$system("g++ -dumpversion")
    contains(GCC_VERSION, 6.[0-9]) {
        message( "g++ version 6.x found" )
        CONFIG += g++6
    } else {
        contains(GCC_VERSION, 5.[0-9]) {
            message( "g++ version 5.x found" )
            CONFIG += g++5
        } else {
            contains(GCC_VERSION, 4.[0-9]) {
                message( "g++ version 4.x found" )
                CONFIG += g++4
            } else {
                message( "Unknown GCC configuration" )
            }
        }
    }
}

As you see you can get version from GCC and then compare it with regex expression.

The way how to use is the same:

SOURCES += blah blah2 blah3
g++4: SOURCES += blah4 blah5
like image 127
dismine Avatar answered Oct 19 '22 23:10

dismine


My answer is based on dismine's answer
We can simply extract the major version number using $$str_member

gcc | clang {
    COMPILER_VERSION = $$system($$QMAKE_CXX " -dumpversion")
    COMPILER_MAJOR_VERSION = $$str_member($$COMPILER_VERSION)
    greaterThan(COMPILER_MAJOR_VERSION, 3): message("gcc 4 or later")
    greaterThan(COMPILER_MAJOR_VERSION, 4): message("gcc 5 or later")
    greaterThan(COMPILER_MAJOR_VERSION, 5): message("gcc 6 or later")
    greaterThan(COMPILER_MAJOR_VERSION, 6): message("gcc 7 or later")
}
like image 25
Eric Lemanissier Avatar answered Oct 19 '22 23:10

Eric Lemanissier


In addition to ashcatch's answer, qmake allows you to query the command line and get the response back as a variable. So you could to something like this:

linux-g++ {
    system( g++ --version | grep -e "\<4.[0-9]" ) {
        message( "g++ version 4.x found" )
        CONFIG += g++4
    }
    else system( g++ --version | grep -e "\<3.[0-9]" ) {
        message( "g++ version 3.x found" )
        CONFIG += g++3
    }
    else {
        error( "Unknown system/compiler configuration" )
    }
}

Then later, when you want to use it to specify targets, you can use the config scoping rules:

SOURCES += blah blah2 blah3
g++4: SOURCES += blah4 blah5
like image 45
Caleb Huitt - cjhuitt Avatar answered Oct 19 '22 23:10

Caleb Huitt - cjhuitt


As a start, I would look at the scoping feature that qmake supports:

Scopes and Conditions

But while I read about it, it seems that by default you can use general platform conditions like win32 or unix or you can use the name of the qmake spec like linux-g++. You could test the Visual Studio version like this (since the different Visual Studio versions use different qmake specs), but I don't think that you can test the gcc version like this (at least I don't know how).

like image 29
ashcatch Avatar answered Oct 20 '22 00:10

ashcatch