#pragma GCC diagnostic ignored "-Wformat"的使用
在GCC下,#pragma GCC diagnostic ignored用于表示關閉診斷警告,忽略診斷問題,
格式:
#pragma GCC diagnostic ignored "-Wformat"
其中-Wformat引數,可以是沒有回傳值的警告"-Wreturn-type",函式未使用的警告"-Wunused-function"等各種警告,可以在編譯時看到GCC所提示的警告,
例如:
test.c: 在函式‘test1’中:
test.c:6:5: 警告: 在有回傳值的的函式中,‘return’不帶回傳值 [-Wreturn-type]
可以看到提示[-Wreturn-type]警告,
詳細的警告串列可以查看Options to Request or Suppress Warnings,
#pragma GCC diagnostic ignored "-Wformat"的示例程式如下:
#include <stdio.h>
/************************************************************************/
int test1(void)
{
return;
}
//關閉警告,診斷忽略該函式沒有回傳值
#pragma GCC diagnostic ignored "-Wreturn-type"
int test2(void)
{
return;
}
/************************************************************************/
int main(int argc, char* argv[])
{
test1();
test2();
return 0;
}
在gcc下編譯
gcc -o test test.c -Wall
函式test1會提示警告不帶回傳值,而函式test2沒有警告,
test.c: 在函式‘test1’中:
test.c:6:5: 警告: 在有回傳值的的函式中,‘return’不帶回傳值 [-Wreturn-type]
如果屏蔽掉
//#pragma GCC diagnostic ignored "-Wreturn-type"
重新編譯,則兩個函式都是提示警告
test.c: 在函式‘test1’中:
test.c:6:5: 警告: 在有回傳值的的函式中,‘return’不帶回傳值 [-Wreturn-type]
test.c: 在函式‘test2’中:
test.c:14:5: 警告: 在有回傳值的的函式中,‘return’不帶回傳值 [-Wreturn-type]
[參考資料]
GCC, the GNU Compiler Collection
GCC, Diagnostic Pragmas
Options to Request or Suppress Warnings
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/143704.html
標籤:其他
