How to suppress several warnings but not all of them coming from libraries?

1.9k views Asked by At

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

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"

Leading to the following error:

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

Or in two lines:

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

But the second one is ignored.

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

EDIT: Here is a 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)));
}

Build with:

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

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

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++.

1

There are 1 answers

0
lalebarde On

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 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.