简体   繁体   English

QT警告级别建议

[英]QT warning level suggestion

What is the warning level you use while compiling QT projects? 编译QT项目时使用的警告级别是多少?

When I compiled with W4, I'm getting a lot of warnings such as: 当我使用W4编译时,我收到了很多警告,例如:

C4127: conditional expression is constant

Should I compile at W3, or find other ways to handle warnings at W4, such as: adding a new header file and using pragma's(mentioned here C++ Coding Standards: 101 Rules, Guidelines, and Best Practices). 我应该在W3编译,还是找到其他方法来处理W4中的警告,例如:添加新的头文件并使用编译指示(此处提到的C ++编码标准:101规则,指南和最佳实践)。

What are your practices? 你的做法是什么?

Thanks. 谢谢。

I ran into the exact same problem you have a couple of years ago, that of setting the compiler to level 4 warnings to catch as many potiential problems as possible. 我遇到了几年前你遇到的完全相同的问题,那就是将编译器设置为4级警告以尽可能多地捕获一些问题。 At the time, I had a support contract with Qt and asked them why their code generated so many warnings. 当时,我与Qt签订了一份支持合同,并问他们为什么他们的代码会产生如此多的警告。 Their response was that they never gaurenteed that their code would compile without any warnings. 他们的反应是,他们从不嘲笑他们的代码会在没有任何警告的情况下编译。 Only that their code would run correctly. 只有他们的代码才能正确运行。

After several attemps, I started surrounding the Qt header files with pragmas to disable the warnings as shown below - 经过几次尝试后,我开始使用pragma包围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

By doing it this way, you only compile the Qt header files at the lower warning level. 通过这种方式,您只能在较低的警告级别编译Qt头文件。 Or whatever level it takes to get rid of the warnings. 或者无论什么级别来摆脱警告。 You may have some individual warnings that still show up, so you could raise the warning level or disable individual warnings with 您可能会有一些单独的警告仍然显示,因此您可以提高警告级别或禁用单个警告

#pragma warning(disable: 4700)

Some Boost library files also have this problem. 一些Boost库文件也有这个问题。

Personally I just use the Makefiles that qmake generates by default... on the presumption that I can trust the guys at Nokia to have it generate Makefiles that do the right thing for the current build environment. 就个人而言,我只是使用qmake默认生成的Makefile ...假设我可以信任诺基亚的人,让它生成Makefiles,为当前的构建环境做正确的事情。

I do see that qmake will take some optional arguments regarding warnings, though: 我确实看到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.

Use CONFIG += warn_on in your .pro file. .pro文件中使用CONFIG += warn_on

See the documentation . 请参阅文档

Option 选项

 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. 

If you're fighting with Q_ASSERT in Visual studio, all that warning push/pop stuff won't work, since macros are "instantiated" in place, far behind you headers. 如果你在Visual Studio中与Q_ASSERT作战,那么所有警告推/弹的东西都不会起作用,因为宏已经“实例化”到位,远远落后于你的标题。 So I would suggest to redefine Q_ASSERT: 所以我建议重新定义Q_ASSERT:

#ifdef NDEBUG
#undef Q_ASSERT
#define Q_ASSERT(x) __noop
#endif

Based on user2846246 's answer, I found that adding the following early on in the compilation of whichever library uses Qt did the trick (in my case that library uses a precompiled header file in Visual Studio so I just added the code to that header file): 根据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

Which is great as I dislike dropping the warning level of a whole library. 这很棒,因为我不喜欢放弃整个图书馆的警告级别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM