Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

gcc

c99

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?

like image 990
vehomzzz Avatar asked Sep 14 '09 18:09

vehomzzz


2 Answers

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.

like image 103
Jonathan Leffler Avatar answered Oct 13 '22 06:10

Jonathan Leffler


Try

-std=gnu99

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

like image 33
Christoph Avatar answered Oct 13 '22 08:10

Christoph