[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Portable printing of 'size_t' (especially with mingw for windows)
From: |
Pádraig Brady |
Subject: |
Re: Portable printing of 'size_t' (especially with mingw for windows) |
Date: |
Thu, 11 Sep 2014 16:48:47 +0100 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130110 Thunderbird/17.0.2 |
On 09/11/2014 04:28 PM, Assaf Gordon wrote:
> Hello,
>
> I'm not sure if this is a gnulib question per-se, but I'm hopeful you could
> perhaps advise me.
>
> I use the following (or similar) code in my project:
>
> size_t line=42;
> error(EXIT_FAILURE, 0, _("error in line %zu"), line);
>
> The "%zu" works for 'size_t' on all unix-like systems I've tried (32 and 64
> bit), and on Cygwin 64bit.
> However, it fails when cross-compiling for windows with mingw (both i868 and
> x86_64 versions), with the following:
>
> warning: unknown conversion type character 'z' in format [-Wformat]
> warning: too many arguments for format [-Wformat-extra-args]
>
> With mingw (due to msvcrt implementation, I assume) the correct type is
> "%Iu", as listed here:
> http://msdn.microsoft.com/en-us/library/tcxf1dw6%28v=vs.71%29.aspx
>
>
> Note that if this had been a "printf" function call, then compiling with
>
> CFLAGS=-D__USE_MINGW_ANSI_STDIO=1
>
> Would have 'fixed' it (or at least worked around it) by substituting 'printf'
> with the internal '__mingw_printf' .
> But because this is 'error()' from gnulib, it is not replaces and therefore
> triggers this error.
>
> I wonder if there's a definition in "inttypes.h" or similar that would be a
> correct one to use,
> or if there's another portable way to compile it.
I've some notes on printing int values at:
http://www.pixelbeat.org/programming/gcc/int_types/
I wasn't aware of the %zu issue TBH and I notice coreutils also uses %zu
You can still apply the casting technique at the above link, like:
error(EXIT_FAILURE, 0, _("error in line %"PRIuMAX), (uintmax_t) line);
cheers,
Pádraig.