编译 QT 项目时使用的警告级别是多少?
当我使用 W4 编译时,我收到了很多警告,例如:
C4127: conditional expression is constant
我应该在 W3 编译,还是在 W4 找到其他处理警告的方法,例如:添加一个新的头文件并使用 pragma's(在此处提到 C++ 编码标准:101 规则、指南和最佳实践)。
你的做法是什么?
谢谢。
编译 QT 项目时使用的警告级别是多少?
当我使用 W4 编译时,我收到了很多警告,例如:
C4127: conditional expression is constant
我应该在 W3 编译,还是在 W4 找到其他处理警告的方法,例如:添加一个新的头文件并使用 pragma's(在此处提到 C++ 编码标准:101 规则、指南和最佳实践)。
你的做法是什么?
谢谢。
几年前我遇到了完全相同的问题,即将编译器设置为 4 级警告以捕获尽可能多的潜在问题。当时,我与 Qt 签订了支持合同,并问他们为什么他们的代码会产生如此多的警告。他们的回答是,他们从不保证他们的代码会在没有任何警告的情况下编译。只有他们的代码才能正确运行。
经过几次尝试,我开始用编译指示包围 Qt 头文件以禁用警告,如下所示 -
#pragma warning(push,3) // drop compiler to level 3 and save current level
#include <QString>
#include <QVariant>
#include <QStack>
#include <QLabel>
#include <QtGui/QTableWidget>
#pragma warning(pop) // restore compiler warning level
通过这样做,您只能在较低的警告级别编译 Qt 头文件。或者消除警告所需的任何级别。您可能仍然会出现一些单独的警告,因此您可以提高警告级别或禁用单独的警告
#pragma warning(disable: 4700)
一些 Boost 库文件也有这个问题。
就我个人而言,我只是使用 qmake 默认生成的 Makefile ......假设我可以相信诺基亚的人让它生成为当前构建环境做正确事情的 Makefile。
不过,我确实看到 qmake 将采用一些关于警告的可选参数:
The level of warning information can be fine-tuned to help you find problems in your project file:
-Wall
qmake will report all known warnings.
-Wnone
No warning information will be generated by qmake.
-Wparser
qmake will only generate parser warnings. This will alert you to common pitfalls and potential problems in the parsing of your project files.
-Wlogic
qmake will warn of common pitfalls and potential problems in your project file. For example, qmake will report whether a file is placed into a list of files multiple times, or if a file cannot be found.
CONFIG += warn_on
在您的.pro
文件中使用。
请参阅文档。
选项
warn_on The compiler should output as many warnings as possible. This is ignored if warn_off is specified. warn_off The compiler should output as few warnings as possible.
如果您在 Visual Studio 中使用 Q_ASSERT,那么所有警告推送/弹出内容都将不起作用,因为宏已“实例化”到位,远远落后于您的标题。所以我建议重新定义Q_ASSERT:
#ifdef NDEBUG
#undef Q_ASSERT
#define Q_ASSERT(x) __noop
#endif
根据user2846246的回答,我发现在编译任何使用 Qt 的库的早期添加以下内容就可以解决问题(在我的情况下,该库在 Visual Studio 中使用预编译的头文件,所以我只是将代码添加到该头文件):
#ifndef _DEBUG
#undef Q_ASSERT
#define Q_ASSERT(x) __noop
#undef Q_ASSERT_X
#define Q_ASSERT_X(cond, where, what) __noop
#endif
这很好,因为我不喜欢降低整个图书馆的警告级别。