如何使g++引起异常而不是对格式错误发出警告

how to make g++ cause an exception instead of warning on format mistake

本文关键字:格式 错误 警告 g++ 何使 异常      更新时间:2023-10-16

我需要强制gcc在这段代码上引发异常而不是警告:

#include <stdio.h>

int main()
{
    printf ("Decimals: %d n", 1977123124L);
    return 0;
}

现在当我运行cmd:

g++ test.cpp -o test.o

test.cpp是一个包含以下代码的文件。

我在输出中发出警告消息:

test.cpp:6: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘long int’

和测试。

我想要的是错误信息和编译失败。

是否有一些标志或其他帮助gcc为我做这件事?

谢谢=)

使用交换机-Werror=format

一般来说,当你有一个警告,你想把它变成错误,使用-Werror=(warning name);在极端情况下,-Werror单独作用会使所有警告变为错误。