学习笔记 #pragma GCC diagnostic ignored “-Wformat“ 的使用

#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值