构建 qmake 项目时如何使用 C++14 功能?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26127217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:31:39  来源:igfitidea点击:

How can I use C++14 features when building qmake projects?

c++qtc++14qmake

提问by Rickforce

I'm currently using C++11 features in my Qt applications. However, I'd like to use some of the new C++14 features in my applications.

我目前在我的 Qt 应用程序中使用 C++11 功能。但是,我想在我的应用程序中使用一些新的 C++14 特性。

To enable C++11 in a Qt application, one only needs to add one line in the qmakeproject file, namely:

要在Qt应用程序中启用C++11,只需要在qmake工程文件中添加一行,即:

CONFIG += c++11

or this for earlier versions:

或者这对于早期版本:

QMAKE_CXXFLAGS += -std=c++1y

I already tried to do the same with C++14, but it didn't work. I changed the above mentioned line of the qmakeproject like this:

我已经尝试用 C++14 做同样的事情,但是没有用。我像这样更改了上面提到的qmake项目的行:

CONFIG += c++14

or this for earlier versions:

或者这对于早期版本:

QMAKE_CXXFLAGS += -std=c++1y

After that, lots of compilation errors, that did not exist before, appear when trying to build the project. The project compiles fine, however, if I try to use any C++14 features, I get a compilation error. This is an example:

之后,在尝试构建项目时出现了许多以前不存在的编译错误。该项目编译正常,但是,如果我尝试使用任何 C++14 功能,则会出现编译错误。这是一个例子:

template<typename T>
constexpr T pi = T(3.1415926535897932385);

This is the corresponding error:

这是相应的错误:

main.cpp:7: error: template declaration of 'constexpr const T pi'
constexpr T pi = T(3.1415926535897932385);  
          ^

How to enable C++14 features when using a qmakeproject in QtCreator?

在 QtCreator 中使用qmake项目时如何启用 C++14 功能?

I am using Qt 5.3.2, Qt Creator 3.2.1, and MinGW 4.8.2 32 bit.

我使用的是 Qt 5.3.2、Qt Creator 3.2.1 和 MinGW 4.8.2 32 位。

采纳答案by Vittorio Romeo

Qt Creatoris just an IDE.

Qt Creator只是一个IDE

You can think of IDEs as "smartertext editors" that aid the developer with debugging, building, code completion, file management and so on.

您可以将 IDE 视为“更智能的文本编辑器”,可帮助开发人员进行调试、构建、代码完成、文件管理等。

IDEs are irrelevant during compilation.

IDE 在编译过程中是无关紧要的。

What matters is your compiler.And it is independentfrom your IDE.

重要的是你的编译器。独立于您的 IDE。

g++ 4.8.xdoes not support many C++14 features: check out this page to learn what C++14 features are supported.

g++ 4.8.x不支持许多 C++14 特性:查看此页面以了解支持哪些 C++14 特性

回答by lpapp

This is now supported properly from Qt 5.4. You just type this into your qmake project file:

这现在从 Qt 5.4 得到正确支持。您只需将其输入到您的 qmake 项目文件中:

CONFIG += c++14

The necessary code change went in the middle of 2014 by Thiago:

Thiago 在 2014 年年中进行了必要的代码更改:

Add support for CONFIG += c++14

添加对 CONFIG += c++14 的支持

I have just created the necessary documentation change:

我刚刚创建了必要的文档更改:

Mention the c++14 CONFIG option in the qmake variable reference

在 qmake 变量参考中提到 c++14 CONFIG 选项

Please note that variable templates are only supported from gcc 5.0, but then your code works just fine. This can be used for testing C++14 with older gcc:

请注意,只有 gcc 5.0 才支持变量模板,但是您的代码可以正常工作。这可用于使用较旧的 gcc 测试 C++14:

main.cpp

主程序

#include <iostream>

int main()
{
    // Binary literals: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3472.pdf
    // Single-quotation-mark as a digit separator: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3781.pdf
    std::cout << 0b0001'0000'0001;
    return 0;
}

main.pro

主程序

TEMPLATE = app
TARGET = main
CONFIG -= qt
CONFIG += c++14
SOURCES += main.cpp

Build and Run

构建和运行

qmake && make && ./main

Output

输出

257

回答by pierre

For some weird reason this is what Qt does: If the compiler is gcc or mingw, CONFIG+=C++14is transformed to a compiler flag -std=c++14(that what you expect)

出于某种奇怪的原因,这就是 Qt 所做的:如果编译器是 gcc 或 mingw,CONFIG+=C++14则转换为编译器标志-std=c++14(您所期望的)

If it's another compiler (like clang), CONFIG=C++14is stupidly transformed to -std=c++11(sic!), so you will get errors about unsupported features, even if your clang version correctly supports C++14.

如果它是另一个编译器(如 clang),CONFIG=C++14则被愚蠢地转换为-std=c++11(原文如此!),因此即使您的 clang 版本正确支持 C++14,您也会收到有关不支持功能的错误。

To fix it, specify the flag explicitly:

要修复它,请明确指定标志:

QMAKE_CXXFLAGS += -std=c++14

This way, you're sure that your compiler (g++, mingw or clang) will receive the correct flags.

这样,您可以确定您的编译器(g++、mingw 或 clang)将收到正确的标志。

回答by hyde

To use C++14 with qmakewith versions before Qt 5.4 (it doesn't make any difference wether you use it with Qt Creatoror with some other IDE or from command line) and gcc, add this to your .profile:

要将 C++14 与qmake与 Qt 5.4 之前的版本一起使用(与Qt Creator或其他 IDE 或从命令行一起使用它没有任何区别)和gcc,请将其添加到您的.pro文件中:

QMAKE_CXXFLAGS += -std=c++1y

qmakegot support for CONFIG += c++14with Qt 5.4, so you can use that for projects where you are comfortable with requiring at least that version of Qt. Be sure to either use the explicit compiler flags, or use the CONFIGoption, but not both at the same time, to avoid conflicting switches.

qmake获得CONFIG += c++14了 Qt 5.4 的支持,因此您可以将其用于您认为至少需要该版本 Qt 的项目。确保使用显式编译器标志或使用CONFIG选项,但不要同时使用两者,以避免冲突切换。



Unfortunately gcc4.8.2 supports very few C++14 features (see here), but you can test that with this code snippet:

不幸的是,gcc4.8.2 支持很少的 C++14 特性(参见这里),但您可以使用以下代码片段进行测试:

#include <iostream>

auto f() { return 42; }

int main()
{
    std::cout << f() << std::endl;
}

That will compile fine with QMAKE_CXXFLAGS += -std=c++1y, but give warning with CONFIG += c++11or QMAKE_CXXFLAGS += -std=c++1x.

使用 可以很好地编译QMAKE_CXXFLAGS += -std=c++1y,但是使用CONFIG += c++11或给出警告QMAKE_CXXFLAGS += -std=c++1x