好记性不如铅笔头

编程

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

本文转自【 https://blog.csdn.net/u012028275/article/details/108763638 】,有删改。
在GCC下,#pragma GCC diagnostic push用于记录当前的诊断状态#pragma GCC diagnostic pop用于恢复诊断状态。可以用于屏蔽局部代码的警告,使用模板如下:

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

使用示例如下:

#include <stdio.h>

/************************************************************************/
//记录当前的诊断状态
#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]

 

发表评论

13 − 4 =

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据