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

StackOverflow https://stackoverflow.com/questions/1423185

  •  07-07-2019
  •  | 
  •  

Question

I have these headers in a c 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?

Was it helpful?

Solution

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.

OTHER TIPS

Try

-std=gnu99

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

You might try -D_BSD_SOURCE to enable BSD-isms or -D_SVID_SOURCE to enable System-V isms

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top