#pragma GCC diagnostic warning "-Wformat"的使用

在GCC下,#pragma GCC diagnostic warning用于表示将诊断视为警告。

格式:

#pragma GCC diagnostic warning "-Wformat"

示例程序如下:

#include <stdio.h>

/************************************************************************/
//关闭警告,诊断忽略该函数没有返回值
#pragma GCC diagnostic ignored "-Wreturn-type"

int test1(void)
{
    return;
}

//开启警告,提示该函数没有返回值
#pragma GCC diagnostic warning "-Wreturn-type"

int test2(void)
{
    return;
}

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

在gcc下编译

gcc -o test test.c -Wall

函数test2会提示警告不带返回值。

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

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

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐