2016-12-29 22 views
0

一些图书馆是越野车,并提出了很多警告,人们不希望在每次编译时都得到这些警告,因为它会淹没有用的应用程序警告和错误。如何禁止几个警告,但不是所有的警告都来自图书馆?

为了抑制一种类型的警告或从图书馆所有这些包括,溶液是hereandrewrjones并回顾这里为了方便地使用(具有一个不同的例子,使其有用):

// 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++的编译选项。它抑制来自crypto ++的所有警告。

+0

如果“第二个被忽略”,我认为你应该写一个[mcve]并将其报告为一个错误。 –

+0

随机猜测:“-Wunused-variable,unused-function”? –

+0

另一个随机猜测:在每个新的“忽略”前添加另一个“#pragma GCC诊断推送”? –

回答

0

下面是适用于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 GCC diagnostic ignored "-Wxxxxx"其中xxxxx是被忽略的警告改变的区域。要忽略几个警告,每个警告都需要一个这样的行。