MinGW的GCC:"未知转换类型字符'H'" (snprintf的) [英] MinGW GCC: "Unknown conversion type character 'h'" (snprintf)

查看:240
本文介绍了MinGW的GCC:"未知转换类型字符'H'" (snprintf的)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我碰到一个奇怪的问题编译使用MinGW(GCC 4.6.2)在Windows 7上C文件有问题的文件包含以下C code:

Okay, I've run into a strange issue compiling a C file with MinGW (GCC 4.6.2) on Windows 7. The file in question contains the following C code:

#include <stdio.h>

int main(int argc, char *argv[]) {
    printf("%2hhX\n", 250);
    char c[80];
    snprintf(c, sizeof(c), "%2hhX", 250);
    printf("%s\n", c);
    return 0;
}

编译原来是这样的:

The compilation turns out like this:

$ gcc.exe -std=c99 -pedantic -Wall test.c
test.c: In function 'main':
test.c:6:2: warning: unknown conversion type character 'h' in format [-Wformat]
test.c:6:2: warning: too many arguments for format [-Wformat-extra-args]

现在,有什么奇怪,我是它抱怨第6行的的snprintf 电话,但不是的printf 呼吁行4.我缺少的东西或者是警告只是不正确的?此外,有没有可能在格式字符串%2hhX更好的相同呢? (我想打印字符变量为十六进制值。)

Now, what's strange to me is that it complains about the snprintf call on line 6, but not the printf call on line 4. Am I missing something or is the warning just incorrect? Also, is there perhaps a better equivalent for the format string "%2hhX"? (I'm trying to print char variables as hexadecimal values.)

推荐答案

从历史上看,MinGW的一直在一点点奇怪的局面,特别是尽可能C99支持一切。 MinGW的主要依赖的是与Windows一起分发的MSVCRT.DLL运行,并且运行时不支持C99。

Historically, MinGW has been in a bit of an odd situation, especially as far as C99 support goes. MinGW relies mostly on the msvcrt.dll runtime that's distributed with Windows, and that runtime doesn't support C99.

因此​​,与旧版本的MinGW的,您可以使用C99指定格式说明时碰上C99模式的问题。另外从历史上看,海湾合作委员会没有为MSVCRT.DLL缺乏对C99符支持任何特殊的住宿。所以,你会陷入其中, -Wformat 不会发出警告,将无法正常工作的格式的情况。

So with older versions of MinGW, you can run into problems in C99 mode when using C99-specific format specifiers. Also historically, GCC didn't make any special accommodations for msvcrt.dll's lack of support for C99 specifiers. So you'd get into situations where -Wformat wouldn't warn about a format that wouldn't work.

情况正在好转两侧 - 海湾合作委员会已与MS运行时使用时-Wformat具体支持,如:

Things are improving on both sides - GCC has specific support for -Wformat when used with the MS runtime, such as:


  • -Wpedantic-MS格式让GCC不会抱怨I32I64(即使它的记录,我仍然得到投诉有关它是无法识别的,即使在4.7.0 - 也许这是全新的)

  • ms_printf 选项 __属性__((__ __格式))

  • -Wpedantic-ms-format so that GCC won't complain about "I32" and "I64" (even though it's documented, I still get a complaint about it being unrecognized even in 4.7.0 - maybe it's brand new)
  • the ms_printf option to __attribute__((__format__))

在另一边,MinGW的提供了自己的的snprintf()一段时间,因为MSVC的变种, _snprintf(),表现完全不同。然而,MinGW的依赖半晌上的的printf()在MSVCRT.DLL,所以C99格式说明的printf()没有工作。在某一点上的MinGW开始提供它自己的版本的printf()和朋友,这样你可以得到适当的C99(和GNU?)的支持。然而,似乎是在保守的一面,这些最初没有更换MSVCRT.DLL的版本。它们有像 __名mingw_printf()

On the other side, MinGW has provided its own snprintf() for a while, since MSVC's variant, _snprintf(), behaves quite differently. However, MinGW relied for a long while on the printf() in msvcrt.dll, so C99 format specifiers for printf() didn't work. At some point MinGW started providing it's own version of printf() and friends so that you could get proper C99 (and GNU?) support. However, it seems that to be on the conservative side, these didn't replace the msvcrt.dll versions initially. They have names like __mingw_printf().

它看起来像在4.6.1和4.7.0之间的某一点,MinGW的头使用的MinGW提供的版本作为替代的MSVCRT.DLL功能(至少如果你specifed C99)开始。

It looks like at some point between 4.6.1 and 4.7.0, the MinGW headers started using the MinGW supplied versions as replacements for the msvcrt.dll function (at least if you've specifed C99).

然而,它似乎与新版本,GCC和MinGW还是有点不同步。哪里像以前GCC不会发出警告符,不会对实际工作的MinGW,不是抱怨spcifiers即会。

However, it seems that with the newer versions, GCC and MinGW are still a little out of sync. Where as before GCC would not warn about specifiers that wouldn't actually work on MinGW, not it complains about spcifiers that will.

您可能想尝试code以下snipet,看看如何好你的MinGW的支持版HHX

You may want to try the following snipet of code to see how well your version of MinGW support "hhX":

printf("%hhX\n", 0x11223344);
__mingw_printf("%hhX\n", 0x11223344);

我不知道什么建议来解决你在运行到这个问题 - 我认为你可能能够修补MinGW的 stdio.h中头等等它有一个 __属性__((__ format__(gnu_printf,...)))上的printf函数属性(他们不存在的新的 stdio.h中,所以GCC将使用它的格式支持是默认的想法)。

I'm not sure what to suggest to fix the problem you're running into - I think that you may be able to patch the MinGW stdio.h header so that it has a __attribute__((__format__ (gnu_printf, ...))) attribute on the printf functions (they're not there in the newer stdio.h, so GCC will use it's default idea of what the format support is).

这篇关于MinGW的GCC:&QUOT;未知转换类型字符'H'&QUOT; (snprintf的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆