如何抑制几个警告,但不是所有警告来自图书馆?

时间:2016-12-29 19:05:32

标签: c++ gcc suppress-warnings

有些库是错误的并且引发了许多警告,每次编译都不希望得到这些警告,因为它会泛滥有用的应用程序警告和错误。

要从库包含抑制一种类型的警告或所有这些警告,解决方案是来自 andrewrjones here,并在此处调用以获得便利性(使用不同的示例使其有用):

// 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可用于抑制所包含库中的所有警告。

但是我怎么能只抑制几个警告,而不是所有警告呢?

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

#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 documentation on these diagnostic pragmas无效。我怎么能这样做?

编辑:这是一个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",现在它可以正常工作!

在我的真实应用中,它总是被忽略。因此,此时,我在编译选项中使用following solution作为解决方法:-isystem/usr/include/crypto++而不是-I/usr/include/crypto++。它禁止来自加密++的所有警告。

1 个答案:

答案 0 :(得分:1)

这是适用于OP的MCVE的解决方案,但由于未知原因而不适用于我的实际应用。

// 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 #pragma GCC diagnostic ignored "-Wxxxxx"更改警告报告的区域,其中xxxxx是要忽略的警告。要忽略几个警告,每个警告都需要一条这样的行。

相关问题