Re: gcc 8.2.0: missing _fileno() for c++11

Keith Marshall <[email protected]> Mon, 20 Jan 2020 22:48:06 +0000
Newsgroups gmane.comp.gnu.mingw.user
Organization MinGW.org Project
Message-ID <[email protected]>
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)
   #endif

   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
   FOR A PARTICULAR PURPOSE.

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

-- 
Regards,
Keith.

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

_______________________________________________
MinGW-Users mailing list
[email protected]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.osdn.me/mailman/listinfo/mingw-users
Also: mailto:[email protected]?subject=unsubscribe
signature.asc (application/pgp-signature, 833 B)
-----BEGIN PGP SIGNATURE-----

iQIzBAEBCAAdFiEEwZ7AGBVH3lDh1I9TwK02xjR+Wj8FAl4mLiYACgkQwK02xjR+
Wj9wlRAAm+TyuSCFdXx6lO5UnAShWmRJ8AE+PI00aonXL0qx2DXzO70c780omX/u
F5NUhbjOGx32+/7tfyTXNDCpvAAba+50yzCERCMmagufu79LIQv9igVwm8Wda4RX
79nfGjd7a+f1W/owzGE6IJLn44qOSfP+gpg3HDIQBTLIxshzkHhzWfYzTuxv2mgd
1PkPfuDiqmraFopnICSWzVOsVjKMXt+6x7a7wIs7vreDDOj3hd0GD56xk51z6enw
e+C+85Q2NlKuZMx/CYXVrFHjHDwZa3siYjN0KTZWWkm62P0zNy71MuEQy7ecXLEL
Nr6+x3nmINVboYjejnO5R8T8caQIbGA7FR+TyPQoRl24yOYPfGhUCufLL5cYlxCY
oqqKOg55fmPZeb8csvsZOK1q1yXE2yPVB5XdIddgr9rS17xE5OeQepml5OrTy0Eg
ye0GXVCAwX36yob+lP9AOR4HBifEeIMOF8i9qBZQFu8BqqzGlAvkD28TFf+wqjCH
Gm8PtxSyloLyTbudBt9SK9UjbtzwDg24hjGD7DIgSRn5OIIHQ1QaKfN3AOikzc4D
nhecxZrsA4J14xHyy3DhZaAfGacJTG+HQh3+GhKcV1NCYtLm/E6weONtYqo4C5Tk
LAgw71pEoIn28n6BWsrO6ZhD84+CxHyfifiK7eQK3Eg3mu7Ndhc=
=Stzp
-----END PGP SIGNATURE-----