views:

563

answers:

4

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. Any ideas are welcome.

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. Thank you all for your answers.

A: 

Each compiler vendor use to define some specific symbols that identify the compiler and version. You could make the check using those symbols.

I know, for example, that _MSC_VER gives the version of Microsoft C++ Compiler.

What I also know is that Boost Libraries use this kind of feature selection and adaptation.

You can take a look to Boost Config headers, found in include folder, at path: boost/config/* , specially at select_compiler_config.hpp.

By using those compiler specific symbols, you can make feature selection at preprocessing phase of building the code.

Cătălin Pitiș
+1  A: 

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).

ashcatch
A: 

The following macros are defined in my version of gcc and g++

#define __GNUC__ 4 
#define __GNUC_MINOR__ 0
#define __GNUC_PATCHLEVEL__ 1

Additionaly the g++ defines:

#define __GNUG__ 4
Martin York
+3  A: 

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
Caleb Huitt - cjhuitt
Perfect thanks. I was thinking on doing something like that but thought I 'd ask if there is something that is already supported out of the box. Since there apparently isn't your solution is ready to use :-)
Yorgos Pagles