如何抑制一些警告,但并非全部来自库? [英] How to suppress several warnings but not all of them coming from libraries?

查看:283
本文介绍了如何抑制一些警告,但并非全部来自库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些库存在错误,并且会发出很多警告,因为每个库都会淹没有用的应用程序警告和错误,因此人们不希望每次编译时都会收到警告.

Some libraries are buggy and raise a lot of warnings one does not want to get at every compilation because it floods usefull own application warnings and errors.

要禁止从库中包含一种或全部警告,解决方案是

To suppress one type of warnings or all of them from libraries includes, the solution is here from andrewrjones and recalled here for conveniance (with a different example to make it usefull):

// Crypt++
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
#include "aes.h"
#include "modes.h"
#include "filters.h"
#include "base64.h"
#include "randpool.h"
#include "sha.h"
#pragma GCC diagnostic pop

andrewrjones 所述,-Wall可用于禁止包含的库中的所有警告.

As explained by andrewrjones, -Wall can be used to suppress all warnings from the included libraries.

但是我该如何只抑制几个警告,而不是全部?

But how can I suppress several warnings only, not all of them?

我已经测试过将它们包含在字符串中,如下所示:

I have tested including them in the string like here:

#pragma GCC diagnostic ignored "-Wunused-variable -Wunused-function"

导致以下错误:

warning: unknown option after '#pragma GCC diagnostic' kind [-Wpragmas]

或分为两行:

#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wunused-function"

但是第二个被忽略了.

关于这些诊断实用程序的 gcc文档没有帮助.我该怎么办?

The gcc documentation on these diagnostic pragmas is not helpful. How could I do this please?

这是MCVE:

#include <string>
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wunused-function"
//#pragma GCC diagnostic ignored "-Wunused-variable,unused-function" // error
//#pragma GCC diagnostic ignored "-Wall"     // does not work
#include "aes.h"
#include "modes.h"
#include "filters.h"
#include "base64.h"
#include "randpool.h"
#include "sha.h"
#pragma GCC diagnostic pop

using namespace std;
using namespace CryptoPP;

int main() {
  byte k[32];
  byte IV[CryptoPP::AES::BLOCKSIZE];
  CBC_Mode<AES>::Decryption Decryptor(k, sizeof(k), IV);
  string cipherText = "*****************";
  string recoveredText;
  StringSource(cipherText, true, new StreamTransformationFilter(Decryptor, new StringSink(recoveredText)));
}

构建方式:

g++ -Wall -I/usr/include/crypto++ -lcrypto++ gdi.cpp

似乎它曾经用来忽略ignored "-Wunused-function",现在它可以工作了!

It appears that it has used to building ignoring the ignored "-Wunused-function", and now it works!

在我的真实应用中,它总是被忽略.因此,在这一点上,我正在使用以下解决方案解决方法:在编译选项中使用-isystem/usr/include/crypto++代替-I/usr/include/crypto++.它禁止来自crypto ++的所有警告.

In my real app, it is always ignored. So, at this point, I am using the following solution as a workaround: -isystem/usr/include/crypto++ instead of -I/usr/include/crypto++ in the compilation options. It suppresses all warnings from crypto++.

推荐答案

以下是适用于OP的MCVE的解决方案,但出于未知原因,不适用于我的实际应用.

Here is the solution which works for the MCVE of the OP, but not in my real application for unknown reasons.

// the following line alters the warning behaviour
#pragma GCC diagnostic push
// put here one line per warning to ignore, here: Wunused-variable and Wunused-function
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wunused-function"
// put here the problematic includes
#include "aes.h"
#include "modes.h"
#include "filters.h"
// the following restores the normal warning behaviour, not affecting other includes
#pragma GCC diagnostic pop

#pragma GCC diagnostic push#pragma GCC diagnostic pop定义了警告报告被嵌入式编译指示#pragma GCC diagnostic ignored "-Wxxxxx"更改的区域,其中xxxxx是要忽略的警告.要忽略多个警告,每个警告都需要一个这样的行.

#pragma GCC diagnostic push and #pragma GCC diagnostic pop define an area where the warning reporting is altered by the embedded pragmas #pragma GCC diagnostic ignored "-Wxxxxx" where xxxxx is the warning to be ignored. To ignore several warnings, one such line is required for each of them.

这篇关于如何抑制一些警告,但并非全部来自库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆