有没有办法通过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.
发布评论
评论(8)
除了 ashcatch 的答案,
qmake
允许您查询命令line 并将响应作为变量返回。 所以你可以这样做:然后,当你想用它来指定目标时,你可以使用配置范围规则:
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:Then later, when you want to use it to specify targets, you can use the config scoping rules:
我的答案基于 Caleb Huitt - cjhuit 的。 但他的方法对我不起作用。
如您所见,您可以从 GCC 获取版本,然后将其与正则表达式进行比较。
使用方法是一样的:
My answer based on Caleb Huitt - cjhuitt's. But his approach does not work for me.
As you see you can get version from GCC and then compare it with regex expression.
The way how to use is the same:
我的答案基于dismine的答案
我们可以使用 $$str_member 简单地提取主版本号
My answer is based on dismine's answer
We can simply extract the major version number using $$str_member
首先,我将查看 qmake 支持的作用域功能:
范围和条件
但是,当我读到它时,似乎默认情况下您可以使用通用平台条件,例如
win32
或unix
或者您可以使用 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
orunix
or you can use the name of the qmake spec likelinux-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).有一个优雅但没有详细记录的功能,如 QT 论坛
QMAKE_GCC_MAJOR_VERSION
和QMAKE_CLANG_MAJOR_VERSION
。 因此,在您的.pro
文件中,您可以编写如下内容:或添加一些
QMAKE_CXXFLAGS
,例如罗伯特·海尔格罗夫 和 Kromignon建议。
There is an elegant but not so well documented feature as described on topic on QT forum
QMAKE_GCC_MAJOR_VERSION
andQMAKE_CLANG_MAJOR_VERSION
. So in your.pro
file you can just write something like:or add some
QMAKE_CXXFLAGS
, e.g.as Robert Hairgrove and Kromignon suggest.
我这样做
它返回“48”。
我用它来链接正确的 boost 库:
有效地给出:
-lboost_system-mgw48-mt-s-1_54
我在 mingw 上。
另一个想法是查看 QMAKESPEC 变量并从中解析,提示:
I do
It returns "48".
I use it for linking proper boost libraries:
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:
每个编译器供应商都使用定义一些标识编译器和版本的特定符号。 您可以使用这些符号进行检查。
例如,我知道 _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.
以下宏是在我的 gcc 和 g++ 版本中定义的,
另外 g++ 还定义了:
The following macros are defined in my version of gcc and g++
Additionaly the g++ defines: