C++ 在 Qt 中添加额外的编译器选项

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

Adding extra compiler option in Qt

c++qtqt-creator

提问by smallB

Where in Qt can I specify additional compiler options? Like for example -std=c++0x?

我可以在 Qt 中的哪里指定额外的编译器选项?例如 -std=c++0x?

回答by Dotti

You can try adding

您可以尝试添加

QMAKE_CXXFLAGS += -std=c++0x

to your .pro file.

到您的 .pro 文件。

However, this should not be used in Qt 5 for enabling specific c++ standard. Instead, c++11or c++14in CONFIGvariable to do that. It will enable GNU extensions (-std=gnu++11), but if that is unwanted, also add strict_c++if you want to disable those. For example, this should pass -std=c++11to the compiler:

但是,这不应在 Qt 5 中用于启用特定的 c++ 标准。相反,c++11c++14CONFIG变量中做到这一点。它将启用 GNU 扩展 (-std=gnu++11),但如果不需要,还可以添加strict_c++是否要禁用它们。例如,这应该传递-std=c++11给编译器:

CONFIG += c++11 strict_c++

回答by Vinicius Kamakura

In your .pro file, you could add:

在您的 .pro 文件中,您可以添加:

QMAKE_CXXFLAGS += -std=c++0x

I think every variable in the spec's qmake.confcan be altered like that.

我认为规范中的每个变量qmake.conf都可以这样更改。

For example, the win32-g++ spec has, among other variables, these:

例如,win32-g++ 规范除了其他变量外,还有以下这些:

QMAKE_CC        = gcc
QMAKE_LEX       = flex
QMAKE_LEXFLAGS      =
QMAKE_YACC      = byacc
QMAKE_YACCFLAGS     = -d
QMAKE_CFLAGS        =
QMAKE_CFLAGS_DEPS   = -M
QMAKE_CFLAGS_WARN_ON    = -Wall
QMAKE_CFLAGS_WARN_OFF   = -w
QMAKE_CFLAGS_RELEASE    = -O2
QMAKE_CFLAGS_DEBUG  = -g
QMAKE_CFLAGS_YACC   = -Wno-unused -Wno-parentheses

QMAKE_CXX       = g++
QMAKE_CXXFLAGS      = $$QMAKE_CFLAGS
QMAKE_CXXFLAGS_DEPS = $$QMAKE_CFLAGS_DEPS
QMAKE_CXXFLAGS_WARN_ON  = $$QMAKE_CFLAGS_WARN_ON
QMAKE_CXXFLAGS_WARN_OFF = $$QMAKE_CFLAGS_WARN_OFF
QMAKE_CXXFLAGS_RELEASE  = $$QMAKE_CFLAGS_RELEASE
QMAKE_CXXFLAGS_DEBUG    = $$QMAKE_CFLAGS_DEBUG
QMAKE_CXXFLAGS_YACC = $$QMAKE_CFLAGS_YACC
QMAKE_CXXFLAGS_THREAD   = $$QMAKE_CFLAGS_THREAD
QMAKE_CXXFLAGS_RTTI_ON  = -frtti
QMAKE_CXXFLAGS_RTTI_OFF = -fno-rtti
QMAKE_CXXFLAGS_EXCEPTIONS_ON = -fexceptions -mthreads
QMAKE_CXXFLAGS_EXCEPTIONS_OFF = -fno-exceptions

回答by kmdent

The way QT deals with compiler options is through the .pro file. It is a double edged sword if I may. It creates a nice abstraction, especially when compiling large projects. The problem is that you have to either look up or memorize how to add the flag. In the case of C++0X, you have to add the following flag to your .pro file:

QT 处理编译器选项的方式是通过 .pro 文件。如果可以的话,这是一把双刃剑。它创建了一个很好的抽象,尤其是在编译大型项目时。问题是您必须查找或记住如何添加标志。对于 C++0X,您必须在 .pro 文件中添加以下标志:

QMAKE_CXXFLAGS += -std=c++0x

Fortunately most of the flags that you need are automatically added if you use QtCreator.

幸运的是,如果您使用 QtCreator,您需要的大部分标志都会自动添加。