[Mingw-users] gcc 8.2.0: missing _fileno() for c++11

Keith Marshall keith****@users*****
Tue Jan 21 07:48:06 JST 2020

On 20/01/2020 21:28, Anton Shepelev wrote:
> Keith Marshall to Anton Shepelev:
>>> On the other hand, this worked with GCC 4.*...
>> GCC-4.x with what options?
> The same test case as posted, but with the GCC I had before
> the upgrade.

Well, I no longer have GCC-4 available, to check its behaviour, but the
__STRICT_ANSI__ restriction is, (and IIRC always has been), imposed
within the mingwrt headers, so any compiling option which activates the
__STRICT_ANSI__ feature test, (as any "-std=..." does), should decline
to support _fileno(), regardless of GCC version.

>> Eli is 100% correct -- -std=c++11 turns on strict ANSI,
>> (or more correctly ISO-C++), conformance checking, and
>> since fileno() is a POSIX.1 extension to ISO-C, (and
>> _fileno() is a Microsoft specific equivalent extension),
>> if you request strict ISO-C/ISO-C++ conformance checking,
>> then either will be rightfully rejected.
> Have you an idea why, then, fileno() is not rejected on
> Linux with -std=c++11?

One can only speculate that GLIBC headers are less rigorous in their
interpretation of __STRICT_ANSI__; opinions may differ on this, but in
mine, that's a defect in GLIBC.

> To be specific -- this program:
>     #include <stdio.h>
>     int main( void )
>     {  FILE *f;
>        f = stdin;
>        fileno( f );
>        return 0;
>     }
> compiles with -std=c++11?  Is it due to a bug or the
> courtesy of developers?

See above.  IMO, this is a GLIBC defect.

>>> May I link it in manually from the MinGW library in
>>> which it *is* implemented?
>> Of course.  You will get it from the default C-runtime
>> library, but you will need to explicitly declare it
>> yourself.
> I had already tried that:
> #include <stdio.h>
> int _fileno( FILE *stream );
> int main( void )
> {  FILE *f;
>    _fileno( f );
>    return 0;
> }
> but the linker fails with:
>     undefined reference to `_fileno(_iobuf*)'

Apologies.  _fileno() is not implemented as a function, in Microsoft's
C-runtime library; it is defined as a macro in <stdio.h>, (or <cstdio>
for use in C++ code).  The following works (tested on Manjaro-Linux,
with my own self-built MinGW cross-compiler, and run under Wine):

   $ cat foo.cc
   #include <cstdio>

   #if defined __MINGW32__ && __STRICT_ANSI__
   int _fileno( FILE * );
   #define _fileno(__F) ((__F)->_file)

   int main()
     FILE *f = stderr;
     int fd = _fileno( f );
     printf( "File descriptor for stderr = %d\n", fd );
     return 0;

   $ mingw32-g++ --version
   mingw32-g++ (MinGW.org Cross-GCC Build-20191117-1) 9.2.0
   Copyright (C) 2019 Free Software Foundation, Inc.
   This is free software; see the source for copying conditions.
   There is NO warranty; not even for MERCHANTABILITY or FITNESS

   $ mingw32-g++ -std=c++11 foo.cc
   $ ./a.exe
   File descriptor for stderr = 2


Public key available from keys.gnupg.net
Key fingerprint: C19E C018 1547 DE50 E1D4 8F53 C0AD 36C6 347E 5A3F

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: OpenPGP digital signature
URL: <https://lists.osdn.me/mailman/archives/mingw-users/attachments/20200120/2d530c2e/attachment-0001.sig>

More information about the MinGW-Users mailing list