_BSD_SOURCE vs. __BSD_VISIBLE

Bruce Evans bde at zeta.org.au
Thu Mar 18 15:55:43 PST 2004


On Thu, 18 Mar 2004, Archie Cobbs wrote:

> All I'm trying to do is write some portable code :-)
>
> Here "portable" simply means that it compiles on both FreeBSD and Linux.
> In order to get the various functions I need I have to set a bunch of flags.
>
> On Linux, these flags do the trick (taken from configure.in):
>
>     AC_DEFINE(_XOPEN_SOURCE, 600, XOpen functions)
>     AC_DEFINE(_GNU_SOURCE, 1, GNU functions)
>     AC_DEFINE(_BSD_SOURCE, 1, BSD functions)
>     AC_DEFINE(_ISOC99_SOURCE, 1, ISO C99 functions)
>     AC_DEFINE(_POSIX_VERSION, 200112, POSIX version)
>     AC_DEFINE(_POSIX_C_SOURCE, 200112, POSIX functions)

Using these tends to restrict potability.  Defining _POSIX_C_SOURCE restricts
visibility to the specified version of POSIX, except some settings of
_XOPEN_SOURCE have precedence and restrict visibility to the specified
version of XSI.  Defining the others gives undefined behaviour; the
actual behaviour is to have no effect under FreeBSD, but apparently these
identifiers are part of the Linux defacto standard so they do something
there.  FreeBSD has _C99_SOURCE, which may work like _ISOC99_SOURCE, and
_ANSI_SOURCE which restricts to C90, and _POSIX_SOURCE which restricts to
the POSIX.1-1990.

> On FreeBSD, _BSD_SOURCE is not recognized, but _POSIX_C_SOURCE is and
> it actually causes the BSD functions to be hidden. So I have to manually
> add __BSD_VISIBLE to get them on FreeBSD.

The only way to get all (Free)BSD functions under FreeBSD is to not
define any identifiers that restrict visibility.

> Q1: Why does Linux require _BSD_SOURCE but FreeBSD requires __BSD_VISIBLE?

Linux apparently requires _BSD_SOURCE to give BSD extensions.  This is
a bug in Linux.  The BSD extensions are better of course :-), and feature
tests macros should generally restrict, not enable extensions.  I think
Linux does this because some BSD extensions are incompatible.

FreeBSD doesn't require __BSD_VISIBLE.  On the contrary, __BSD_VISIBLE is
an implementation detail, and setting it in applications gives undefined
behaviour.

> Q2: Is there some standard for these flags documented somewhere?

_POSIX_C_SOURCE and _XOPEN_SOURCE are documented in the POSIX.1-2001 standard.
So is _POSIX_VERSION, but it is quite different (the system defines it).
The others are nonstandard.

> Q3: What is the right and portable set of flags to use?

There is none.  For FreeBSD, you probably need a null set of flags.
For Linux, this is the wrong list to ask on :-).

Bruce


More information about the freebsd-standards mailing list