einpoklum
einpoklum

Reputation: 131976

_POSIX_ vs _POSIX_SOURCE vs _POSIX_C_SOURCE

(split this off from this question)

The following 3 macros appear in many C source files which try to detect/rely on the availability of POSIX functionality:

In the linked-to question it's explained that we're supposed to set _POSIX_C_SOURCE to the POSIX version on which we want to rely (although setting it doesn't guarantee that the functionality will actually be available - that's up to the compiler/OS). Also, while I can set it as the user, it's not clear when others set it themselves (e.g. the compiler / build system). For the other two, I know neither when I should set them nor when others set them.

So, what is the difference in meaning between the three macros? When would each of them be set for me? And why/when should I choose to set one of them over the others, if at all?

Upvotes: 3

Views: 936

Answers (1)

einpoklum
einpoklum

Reputation: 131976

Very partial answer (and with thanks to @SomeProgrammerDude):


The POSIX reference tells us that:

The POSIX.1-1990 standard specified a macro called _POSIX_SOURCE. This has been superseded by _POSIX_C_SOURCE.

and in practical terms, the GNU C library manual tells us, for example:

The state of _POSIX_SOURCE is irrelevant if you define the macro _POSIX_C_SOURCE to a positive integer.

and it should probably be the same for other C standard library implementations. So - never use _POSIX_SOURCE yourself, only use _POSIX_C_SOURCE (except if you are on old platforms where the OS and libraries have not seen updates for the past 20 years at least).


_POSIX_ and _POSIX is a Microsoft-Visual-C(++)-specific macro. I am guessing you define it to get MSVC to expose POSIX/POSIX-like functionality. According to this non-authoritative thread on the MinGW mailing list, MSVC no longer uses _POSIX_ (and _POSIX?) as of MSVC2013.

Upvotes: 3

Related Questions