_POSIX_C_SOURCE

On GNOME bug 561962 I have someone complaining that Metacity defines _POSIX_C_SOURCE before using POSIX functions (as you’re supposed to). He says that defining this breaks the build on OS X. Do some of you know much about POSIX on OS X and can explain to me why this should be, here or on the bug? It seems to me that if your toolchain knows about _POSIX_C_SOURCE then you’ll need it to use POSIX-only functions, and if it doesn’t it’s just another symbol. I can’t see why removing it would fix something.

6 Responses to “_POSIX_C_SOURCE”

  1. What, so on OS X, _POSIX_C_SOURCE means “I want to use POSIX and I don’t want to use any *non*-POSIX calls?”

  2. “_POSIX_C_SOURCE means ‘I want to use POSIX and I don’t want to use any *non*-POSIX calls?’”

    I guess not only on OSX, but on all compliant POSIX sources. From the spec: “Additional symbols not required or explicitly permitted by IEEE Std 1003.1-2001 to be in that header shall not be made visible, except when enabled by another feature test macro.”

    I guess on OSX that “enabled by another feature test macro” thing is the “_DARWIN_C_SOURCE” macro.

  3. meh says:

    The compilation is stopping on a line like

    #if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE == 1

    Try changing your #define to something like

    #define _POSIX_C_SOURCE 1

    which is probably what you wanted in the first place.

  4. Jan Hauffa says:

    On my MacOS 10.5 system, sys/cdefs.h contains a few lines that look like this:

    #if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE == xyz

    This only works if _POSIX_C_SOURCE has a value.

    http://opengroup.org/onlinepubs/009695399/functions/xsh_chap02_02.html

    The above link suggests that _POSIX_C_SOURCE should be defined to have the value 200112L.

  5. Dan says:

    The problem is that _POSIX_C_SOURCE is stupid, and doesn’t mean “I need POSIX features”. It means “I need POSIX features and I *don’t* need non-POSIX features. Basically this means that you only use _POSIX_C_SOURCE when you want to treat all platforms as being completely identical. If you are using autoconf, then pretty much by definition you don’t want to use _POSIX_C_SOURCE. (Yes, this means _POSIX_C_SOURCE is completely useless in the real world. It’s not meant for the real world, it’s meant for POSIX-compliance test suites.)

    If you get a “not defined” warning on linux when you remove _POSIX_C_SOURCE, then set _GNU_SOURCE instead.