C++ 在 Qt、QtCreator 和 QMake 中配置 GCC 编译器开关

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2987062/
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:42:14  来源:igfitidea点击:

Configuring the GCC compiler switches in Qt, QtCreator, and QMake

c++qtc++11qt-creator

提问by andand

I recently tried to use Qt Creator1.3.2, Qt 4.6.2, and GCC 4.4.0 (32-bit version) on Windows 7 (64-bit) to compile an application using some of the experimental C++0x extensions and encountered the following (fatal) error:

我最近尝试在 Windows 7(64 位)上使用Qt Creator1.3.2、Qt 4.6.2 和 GCC 4.4.0(32 位版本)来编译使用一些实验性 C++0x 扩展和遇到以下(致命)错误:

This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.

此文件需要对即将推出的 ISO C++ 标准 C++0x 的编译器和库支持。此支持目前处于实验阶段,必须使用 -std=c++0x 或 -std=gnu++0x 编译器选项启用。

In my search for a solution, I came across the thread qmake and compiler flags?, and added the following to the .pro file:

在寻找解决方案时,我遇到了线程qmake 和编译器标志?,并将以下内容添加到 .pro 文件中:

CXXFLAGS += -std=c++0x

but that didn't seem to make a difference.

但这似乎没有什么区别。

So, I expect there's some tag I need to add to the .pro (project) file, but I've never messed with the GCC compiler switches in Qt, QMake, and QtCreator before, and I am uncertain about the proper invokation / incantation. So, my question is how do you set GCC compiler switches when using QtCreator, QMake, and Qt?

所以,我希望有一些标签需要添加到 .pro(项目)文件中,但我以前从未在 Qt、QMake 和 QtCreator 中使用过 GCC 编译器开关,而且我不确定正确的调用/咒语. 所以,我的问题是在使用 QtCreator、QMake 和 Qt 时如何设置 GCC 编译器开关?

回答by andand

It boils down to reading the manual. Instead of using CXXFLAGSin the .pro file, you need to use QMAKE_CXXFLAGSas in:

归结为阅读手册。而不是CXXFLAGS在 .pro 文件中使用,您需要使用QMAKE_CXXFLAGS

main.cpp:

主.cpp:

#include <cinttypes>

int main() { return 0; }

main.pro:

main.pro:

SOURCES += main.cpp
QMAKE_CXXFLAGS += -std=c++0x

回答by doug65536

You should use

你应该使用

CONFIG += c++11

to enable C++11 compiler flags automatically.

自动启用 C++11 编译器标志。

Look for .prffiles in your qt installation. I don't know where they might be on windows, but on my Linux installation they are under /opt/Qt/5.4/gcc_64/mkspecs/features.

.prf在您的 qt 安装中查找文件。我不知道它们在 Windows 上的位置,但在我的 Linux 安装中,它们位于/opt/Qt/5.4/gcc_64/mkspecs/features.

You might want to read the qmake documentationfor that:

您可能需要阅读qmake 文档

qmake can be set up with extra configuration features that are specified in feature (.prf) files. These extra features often provide support for custom tools that are used during the build process. To add a feature to the build process, append the feature name (the stem of the feature filename) to the CONFIG variable.

可以使用特性 (.prf) 文件中指定的额外配置特性来设置 qmake。这些额外的功能通常为在构建过程中使用的自定义工具提供支持。要将功能添加到构建过程,请将功能名称(功能文件名的词干)附加到 CONFIG 变量。

You can add your own features.

您可以添加自己的功能

Here is what I found on my system. CONFIG += namewill enable the feature:

这是我在我的系统上找到的。CONFIG += name将启用该功能:

./android/android_deployment_settings.prf
./android/android.prf
./build_pass.prf
./c++11.prf
./c++14.prf
./cmake_functions.prf
./configure.prf
./create_cmake.prf
./ctest_testcase_common.prf
./ctest_testcase_installed.prf
./ctest_testcase.prf
./dbusadaptors.prf
./dbusinterfaces.prf
./declarative_debug.prf
./default_post.prf
./default_pre.prf
./designer_defines.prf
./device_config.prf
./egl.prf
./exceptions_off.prf
./exceptions.prf
./exclusive_builds_post.prf
./exclusive_builds.prf
./gcov.prf
./include_source_dir.prf
./incredibuild_xge.prf
./java.prf
./lex.prf
./link_ltcg.prf
./link_pkgconfig.prf
./ltcg.prf
./mac/default_post.prf
./mac/default_pre.prf
./mac/objective_c.prf
./mac/rez.prf
./mac/sdk.prf
./moc.prf
./no_debug_info.prf
./precompile_header.prf
./qfeatures.prf
./qlalr.prf
./qml1_module.prf
./qml1_plugin.prf
./qml_debug.prf
./qml_module.prf
./qml_plugin.prf
./qmltestcase.prf
./qpa/basicunixfontdatabase.prf
./qpa/genericunixfontdatabase.prf
./qt_android_deps.prf
./qt_app.prf
./qt_build_config.prf
./qt_build_paths.prf
./qt_common.prf
./qt_config.prf
./qt_docs.prf
./qt_docs_targets.prf
./qt_example_installs.prf
./qt_functions.prf
./qt_headersclean.prf
./qt_helper_lib.prf
./qt_installs.prf
./qt_module_headers.prf
./qt_module.prf
./qt_module_pris.prf
./qt_parts.prf
./qt_plugin.prf
./qt.prf
./qt_targets.prf
./qt_tool.prf
./resolve_config.prf
./resolve_target.prf
./resources.prf
./silent.prf
./simd.prf
./spec_post.prf
./spec_pre.prf
./testcase.prf
./testcase_targets.prf
./testcocoon.prf
./testlib_defines.prf
./uic.prf
./unix/bsymbolic_functions.prf
./unix/dylib.prf
./unix/hide_symbols.prf
./unix/largefile.prf
./unix/opengl.prf
./unix/openvg.prf
./unix/separate_debug_info.prf
./unix/thread.prf
./unix/x11inc.prf
./unix/x11lib.prf
./unix/x11.prf
./unix/x11sm.prf
./use_c_linker.prf
./vxworks.prf
./warn_off.prf
./warn_on.prf
./wayland-scanner.prf
./win32/console.prf
./win32/default_pre.prf
./win32/dumpcpp.prf
./win32/idcidl.prf
./win32/msvc_mp.prf
./win32/opengl.prf
./win32/openvg.prf
./win32/qt_config.prf
./win32/qt_dll.prf
./win32/rtti_off.prf
./win32/rtti.prf
./win32/stl_off.prf
./win32/stl.prf
./win32/windeployqt.prf
./win32/windows.prf
./winrt/console.prf
./winrt/font_deployment.prf
./winrt/package_manifest.prf
./yacc.prf

回答by Doug Royer

The only way that really works for me is to add it toQMAKE_CXXFLAGS.

真正对我有用的唯一方法是将它添加到QMAKE_CXXFLAGS.

The CONFIG += c++11does not add-std=c++11to the compile command.

CONFIG += c++11不添加-std=c++11到编译命令。