简体   繁体   中英

fileno, F_LOCK and F_ULOCK become undeclared and unavailable when I add std=c99 flag to gcc

I have these headers in ac code

#include <stdio.h>
#include <unistd.h>

Everything compiled fine until I added -std=c99 flag to gcc command (to enable restrict ). And this triggered the following errors.

warning: implicit declaration of function fileno

error: F_LOCK undeclared (first use in this function)
error: (Each undeclared identifier is reported only once error: for each function it appears in.)
error: F_ULOCK undeclared (first use in this function

Any ideas to workaround these errors/warnings?

You need to tell the headers that you want the POSIX extensions. These days, I use:

#if __STDC_VERSION__ >= 199901L
#define _XOPEN_SOURCE 600
#else
#define _XOPEN_SOURCE 500
#endif /* __STDC_VERSION__ */

If I'm compiling with -std=c89 , it gives the correct POSIX version; if you compile with -std=c89 , it gives the correct POSIX version. I use this on Solaris 9 and 10, MacOS X (10.4.x, 10.5.x), HP-UX 11.x, Linux (RHEL4 and 5, SuSE 9 and 10) and AIX 5.x and 6.x - AFAICR, without problems so far.

This stanza should appear before any system headers are included (in your own header, or in each source file), or you need to achieve the same effect with -D_XOPEN_SOURCE=600 on the compiler command line, or some other similar mechanism.

Try

-std=gnu99

to enable all extensions and still use the C99 language enhancements.

您可以尝试-D_BSD_SOURCE启用BSD-isms或-D_SVID_SOURCE启用System-V主题

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM