使用 qmake 查找编译器供应商/版本

发布于 2024-07-18 03:28:52 字数 182 浏览 4 评论 0 原文

有没有办法通过qmake获取用户使用的编译器的版本和供应商? 我需要的是在使用 g++ 3.x 时禁用构建项目的某些目标,并在使用 g++ 4.x 时启用它们。

更新:大多数答案都针对预处理器。 这是我想避免的事情。 我不希望为特定编译器版本构建目标,并且我希望由构建系统做出此决定。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

情释 2024-07-25 03:28:53

除了 ashcatch 的答案,qmake 允许您查询命令line 并将响应作为变量返回。 所以你可以这样做:

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" )
    }
}

然后,当你想用它来指定目标时,你可以使用配置范围规则:

SOURCES += blah blah2 blah3
g++4: SOURCES += blah4 blah5

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
南风起 2024-07-25 03:28:53

我的答案基于 Caleb Huitt - cjhuit 的。 但他的方法对我不起作用。

*-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" )
            }
        }
    }
}

如您所见,您可以从 GCC 获取版本,然后将其与正则表达式进行比较。

使用方法是一样的:

SOURCES += blah blah2 blah3
g++4: SOURCES += blah4 blah5

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
是伱的 2024-07-25 03:28:53

我的答案基于dismine的答案
我们可以使用 $$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")
}

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")
}
£冰雨忧蓝° 2024-07-25 03:28:53

首先,我将查看 qmake 支持的作用域功能:

范围和条件

但是,当我读到它时,似乎默认情况下您可以使用通用平台条件,例如 win32unix 或者您可以使用 qmake 规范的名称,例如 linux-g++。 您可以像这样测试 Visual Studio 版本(因为不同的 Visual Studio 版本使用不同的 qmake 规范),但我认为您不能像这样测试 gcc 版本(至少我不知道如何)。

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

独闯女儿国 2024-07-25 03:28:53

有一个优雅但没有详细记录的功能,如 QT 论坛 QMAKE_GCC_MAJOR_VERSIONQMAKE_CLANG_MAJOR_VERSION。 因此,在您的 .pro 文件中,您可以编写如下内容:

linux-g++:greaterThan(QMAKE_GCC_MAJOR_VERSION, 8):message( "g++ version 9+ found" )
else:linux-clang:greaterThan(QMAKE_CLANG_MAJOR_VERSION, 13):message( "clang version 14+ found" )

或添加一些 QMAKE_CXXFLAGS,例如

linux-g++:greaterThan(QMAKE_GCC_MAJOR_VERSION, 7):QMAKE_CXXFLAGS += -Wno-deprecated-copy

罗伯特·海尔格罗夫Kromignon建议。

There is an elegant but not so well documented feature as described on topic on QT forum QMAKE_GCC_MAJOR_VERSION and QMAKE_CLANG_MAJOR_VERSION. So in your .pro file you can just write something like:

linux-g++:greaterThan(QMAKE_GCC_MAJOR_VERSION, 8):message( "g++ version 9+ found" )
else:linux-clang:greaterThan(QMAKE_CLANG_MAJOR_VERSION, 13):message( "clang version 14+ found" )

or add some QMAKE_CXXFLAGS, e.g.

linux-g++:greaterThan(QMAKE_GCC_MAJOR_VERSION, 7):QMAKE_CXXFLAGS += -Wno-deprecated-copy

as Robert Hairgrove and Kromignon suggest.

甜宝宝 2024-07-25 03:28:53

我这样做

isEmpty(MGWVER) {
    MGW_MAJ = $system(echo | gcc -dM -E - | fgrep __GNUC__ | cut -d\" \" -f 3)
    MGW_MIN = $system(echo | gcc -dM -E - | fgrep __GNUC_MINOR__ | cut -d\" \" -f 3)
    MGWVER =$MGW_MAJ$MGW_MIN
    message(MGWVER $MGWVER)
}

它返回“48”。
我用它来链接正确的 boost 库:

USER_BOOST_CFG=mgw${MGWVER}-mt-s-$(BOOST_VER)
message($USER_BOOST_CFG)
LIBS *= -L$(BOOST_ROOT)/lib
LIBS *= -L$(BOOST_ROOT)/stage/lib

LIBS *= -lboost_system-$USER_BOOST_CFG
LIBS *= -lboost_filesystem-$USER_BOOST_CFG
LIBS *= -lboost_date_time-$USER_BOOST_CFG

有效地给出:
-lboost_system-mgw48-mt-s-1_54

我在 mingw 上。

另一个想法是查看 QMAKESPEC 变量并从中解析,提示:

message(QMAKESPEC $QMAKESPEC)
SPLITED=$section(QMAKESPEC, "/", 0, -3)
message(SPLITED $SPLITED)

I do

isEmpty(MGWVER) {
    MGW_MAJ = $system(echo | gcc -dM -E - | fgrep __GNUC__ | cut -d\" \" -f 3)
    MGW_MIN = $system(echo | gcc -dM -E - | fgrep __GNUC_MINOR__ | cut -d\" \" -f 3)
    MGWVER =$MGW_MAJ$MGW_MIN
    message(MGWVER $MGWVER)
}

It returns "48".
I use it for linking proper boost libraries:

USER_BOOST_CFG=mgw${MGWVER}-mt-s-$(BOOST_VER)
message($USER_BOOST_CFG)
LIBS *= -L$(BOOST_ROOT)/lib
LIBS *= -L$(BOOST_ROOT)/stage/lib

LIBS *= -lboost_system-$USER_BOOST_CFG
LIBS *= -lboost_filesystem-$USER_BOOST_CFG
LIBS *= -lboost_date_time-$USER_BOOST_CFG

effectively giving:
-lboost_system-mgw48-mt-s-1_54

I am on mingw.

Another idea is to look at QMAKESPEC vaariable and parse from it, hint:

message(QMAKESPEC $QMAKESPEC)
SPLITED=$section(QMAKESPEC, "/", 0, -3)
message(SPLITED $SPLITED)
旧伤还要旧人安 2024-07-25 03:28:53

每个编译器供应商都使用定义一些标识编译器和版本的特定符号。 您可以使用这些符号进行检查。

例如,我知道 _MSC_VER 给出了 Microsoft C++ 编译器的版本。

我还知道 Boost Libraries 使用这种特征选择和适应。

您可以查看 Boost Config 标头,该标头位于包含文件夹中,路径: boost/config/* ,特别是 select_compiler_config.hpp 。

通过使用这些编译器特定的符号,您可以在构建代码的预处理阶段进行功能选择。

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.

ぺ禁宫浮华殁 2024-07-25 03:28:53

以下宏是在我的 gcc 和 g++ 版本中定义的,

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

另外 g++ 还定义了:

#define __GNUG__ 4

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文