_GNU_SOURCE, _BSD_SOURCE, and __FAVOR_BSD

Mikel Ward mikel@mikelward.com
Tue Apr 19 12:13:00 GMT 2011


http://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html#Feature-Test-Macros

says:

If you want to get the full effect of _GNU_SOURCE but make the BSD
definitions take precedence over the POSIX definitions, use this
sequence of definitions:

          #define _GNU_SOURCE
          #define _BSD_SOURCE
          #define _SVID_SOURCE

But AFAICT, the order is unimportant.

The only code related to this that I can find is:

/* If _BSD_SOURCE was defined by the user, favor BSD over POSIX.  */
#if defined _BSD_SOURCE && \
    !(defined _POSIX_SOURCE || defined _POSIX_C_SOURCE || \
      defined _XOPEN_SOURCE || defined _GNU_SOURCE || defined _SVID_SOURCE)
# define __FAVOR_BSD    1
#endif

in <features.h>.

Which suggests we get BSD definitions only if no other standard is
also specified.

As a test:

$ gcc -E - >gnubsd.inc <<EOF
> #define _GNU_SOURCE
> #define _BSD_SOURCE
> #define _SVID_SOURCE
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <unistd.h>
> EOF

and

$ gcc -E - >bsdgnu.inc <<EOF
> #define _BSD_SOURCE
> #define _GNU_SOURCE
> #define _SVID_SOURCE
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <unistd.h>
> EOF

Produce the same file except for trivial line number differences such as
< # 9 "<stdin>"

I was not relying on any difference, but was wondering what the best
include order was for portability, and whether the documentation is
correct.

http://sourceware.org/git/?p=glibc.git;a=blob;f=manual/creature.texi;h=96501568a0bbd1be26f0f0f733c2bb432988a712;hb=HEAD

Thanks for any feedback.

Mikel



More information about the Libc-help mailing list