tags:

views:

223

answers:

3

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?

+4  A: 

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.

Jonathan Leffler
+1  A: 

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

Chris Dodd
Use `-D_DARWIN_C_SOURCE` on MacOS X; `-D__EXTENSIONS__` on Solaris; `-D_GNU_SOURCE` on Linux; and the list continues (not sure what the equivalents are on HP-UX and AIX, but they exist, almost certainly).
Jonathan Leffler
+1  A: 

Try

-std=gnu99

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

Christoph