学习笔记 #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 <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]

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

  • 7
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值