学习笔记 #pragma GCC diagnostic push 与 #pragma GCC diagnostic pop 的使用

#pragma GCC diagnostic push#pragma GCC diagnostic pop的使用

在GCC下,#pragma GCC diagnostic push用于记录当前的诊断状态,#pragma GCC diagnostic pop用于恢复诊断状态。

可以用于屏蔽局部代码的警告

#pragma GCC diagnostic push 
#pragma GCC diagnostic ignored "-Wformat" 
//code
#pragma GCC diagnostic pop

使用示例如下:

#include 

/************************************************************************/
//记录当前的诊断状态
#pragma GCC diagnostic push
//关闭警告,诊断忽略没有返回值
#pragma GCC diagnostic ignored "-Wreturn-type"

int test1(void)
{
    return;
}
//恢复到之前的诊断状态
#pragma GCC diagnostic pop

int test2(void)
{
    return;
}

/************************************************************************/
int main(int argc, char* argv[])
{
    test1();
    test2();
    
    return 0;
}

在gcc下编译

gcc -o test test.c -Wall

函数test2会提示警告不带返回值,而函数test1没有警告。

test.c: 在函数‘test2’中:
test.c:18:5: 警告: 在有返回值的的函数中,‘return’不带返回值 [-Wreturn-type]

[参考资料]
GCC, the GNU Compiler Collection
GCC, Diagnostic Pragmas
#pragma GCC diagnostic ignored "-Wformat"功能介绍

你可能感兴趣的:(学习笔记,c语言,gnu)