各个层次的gcc警告
從上到下覆蓋
變量(代碼)級:指定某個變量警告
int a __attribute__ ((unused));
指定該變量為"未使用的".即使這個變量沒有被使用,編譯時也會忽略則個警告輸出.
文件級:在源代碼文件中診斷(忽略/警告)
語法:
#pragma GCC diagnostic [error|warning|ignored] "-W<警告選項>"
#pragma ?GCC diagnostic ignored ?"-Wunused"
#pragma ?GCC diagnostic ignored ?"-Wunused-parameter"
#pragma ?GCC diagnostic warning ?"-Wunused"
#pragma ?GCC diagnostic warning ?"-Wunused-parameter"
#pragma ?GCC diagnostic error ?"-Wunused"
#pragma ?GCC diagnostic error ?"-Wunused-parameter"
在文件開頭處關閉警告,在文件結尾出再開啟警告,這樣可以忽略該文件中的指定警告.
項目級:命令行/編譯參數指定
警告:
gcc main.c -Wall 忽略:
gcc mian.c -Wall -Wno-unused-parameter //開去all警告,但是忽略 -unused-parameter警告
選項格式: -W[no-]<警告選項>
如 : -Wno-unused-parameter # no- 表示診斷時忽略這個警告
來源:https://github.com/zodiac1111/note/blob/master/note/gcc/gcc-ignored-warning.markdown#%E6%96%87%E4%BB%B6%E7%BA%A7%E5%9C%A8%E6%BA%90%E4%BB%A3%E7%A0%81%E6%96%87%E4%BB%B6%E4%B8%AD%E8%AF%8A%E6%96%AD%E5%BF%BD%E7%95%A5%E8%AD%A6%E5%91%8A
6.59.10 Diagnostic Pragmas
GCC allows the user to selectively enable or disable certain types of diagnostics, and change the kind of the diagnostic. For example, a project's policy might require that all sources compile with?-Werror?but certain files might have exceptions allowing specific types of warnings. Or, a project might selectively enable diagnostics and treat them as errors depending on which preprocessor macros are defined.
#pragma GCC diagnostic?kind?optionkind?is ‘error’ to treat this diagnostic as an error, ‘warning’ to treat it like a warning (even if?-Werror?is in effect), or ‘ignored’ if the diagnostic is to be ignored.?option?is a double quoted string that matches the command-line option.
<span style="font-size:18px;"> #pragma GCC diagnostic warning "-Wformat"#pragma GCC diagnostic error "-Wformat"#pragma GCC diagnostic ignored "-Wformat" </span>Note that these pragmas override any command-line options. GCC keeps track of the location of each pragma, and issues diagnostics according to the state as of that point in the source file. Thus, pragmas occurring after a line do not affect diagnostics caused by that line.?
GCC also offers a simple mechanism for printing messages during compilation.
#pragma message?stringstring?may be parenthesized, and is printed with location information. For example,
<span style="font-size:18px;"> #define DO_PRAGMA(x) _Pragma (#x)#define TODO(x) DO_PRAGMA(message ("TODO - " #x))TODO(Remember to fix this) </span>prints ‘/tmp/file.c:4: note: #pragma message: TODO - Remember to fix this’.
總結
以上是生活随笔為你收集整理的各个层次的gcc警告的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2017招行网上申请信用卡流程
- 下一篇: CMake 手册详解(二十二)