2016-12-29 23 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是被忽略的警告改變的區域。要忽略幾個警告,每個警告都需要一個這樣的行。