简体   繁体   中英

Suppress GCC warnings

I have code:

#ifdef Q_OS_LINUX
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcomment"
#include "header.h"
#pragma GCC diagnostic pop
#endif

And I want to supress GCC warning messages related to header.h and all headers included from header.h. But I still have '-Wcomment' warnings related to headers included from header.h. How can I avoid that? Thanks

gcc 4.8.2

edit: The Warning I get looks like this:

/------ Set Analog Output for 8022/8026 --------- / Exp8K WORD CALLBACK AnalogOutHex_8K(DWORD dwBuf[], float fBuf[], warning: "/ " within comment [-Wcomment] No other pragmas surely. -Wall doesn't work

GCC warnings that are emitted by the preprocessor cannot be suppressed with any pragma when compiling C++, they can only be suppressed by pragmas when compiling C. You're compiling as C++ (and shouldn't have tagged your question as C too). Here's a simple test case:

#pragma GCC diagnostic ignored "-Wcomment"
/* /* */

This warns in C++ mode, but not in C mode.

Given that pragmas just won't work, you should take some other approach. If you can modify the header, just change the comment. If you cannot change the header, you can mark the specific directory the header is in as a system header directory (use the -isystem command-line option).

If you can modify header.h , you could define it to be a system header using #pragma GCC system_header . Otherwise, you could add it to your gcc command line using -isystem .

All warnings, other than those generated by '#warning' (see Diagnostics), are suppressed while GCC is processing a system header.

You have not included your full cpp file. My guess is that an earlier include is already including some of the header files. Those header files will probably have header guards which prevent the header file being included more than once. And therefore the #pragma is not really doing anything.

You best bet is to move the #pragmas and include to the top of your header file before you include anything else. Remember you can also push and pop diagnostic pragmas.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM