首页 > 解决方案 > mingw 环境中 64 位字符串格式化程序的警告

问题描述

在最新版本的 mingw-w64 上避免 64 位 printf/scanf 参数警告的正确方法是什么?

我知道 mingw-w64 的许多标准库函数依赖于 microsoft 运行时,并且这些函数与 C99 标准不兼容。

旧答案说要使用%I64d,但由于 C99 标准是%lld,无论如何,如果我使用-Wall -pedantic编译,我会收到两种语法的警告,这是一个小例子:

#include <stdio.h>

int main(void)
{
    long long test;
    scanf("%I64d", &test);
    scanf("%lld", &test);
}

用gcc编译(我的版本是mingw-w64 5.0.4,gcc 8.2.0):

x86_64-w64-mingw32-gcc -o test.exe test.c -Wall -pedantic

它给出以下警告:

dev:tmp dev$ x86_64-w64-mingw32-gcc -o test.exe test.c -Wall -pedantic
test.c: In function ‘main’:
test.c:6:11: warning: ISO C does not support the ‘I64’ ms_scanf length modifier [-Wformat=]
     scanf("%I64d", &test);
           ^~~~~~~
test.c:6:11: warning: ISO C does not support the ‘I64’ ms_scanf length modifier [-Wformat=]
test.c:7:14: warning: unknown conversion type character ‘l’ in format [-Wformat=]
     scanf("%lld", &test);
              ^
test.c:7:11: warning: too many arguments for format [-Wformat-extra-args]
     scanf("%lld", &test);
           ^~~~~~
test.c:7:14: warning: unknown conversion type character ‘l’ in format [-Wformat=]
     scanf("%lld", &test);
              ^
test.c:7:11: warning: too many arguments for format [-Wformat-extra-args]
     scanf("%lld", &test);
           ^~~~~~

删除-Wall删除两个警告,没有-pedantic我可以在没有警告的情况下编译第 6 行 (%I64d) 但不能编译第 7 行 (%lld)。

标签: cscanfmingw-w64

解决方案


如果你想编译到微软平台,你不能拘泥于遵循标准。拥抱,扩展(和熄灭)!删除-pedantic或获取正确的 C99/C11/C17 实现。


C89 不支持%lld-Wformat如果您碰巧将支持它作为扩展的库作为目标,您似乎需要禁用。


推荐阅读