POSIX Threads: Semi-FAQ Revision 5.2
© 2001-2006 Michael M. Lampkin
email: michael.lampkin<at>ieee.org
© 2001-2006 Michael M. Lampkin
email: michael.lampkin<at>ieee.org
POSIX is an internal standard published by the IEEE that is loosely based on UNIX System V and Berkeley Unix that defines the basic interfaces, services and utilities of compliant operating systems. Its primary aim is to facilitate the portability of applications across many systems.
POSIX.1 was first release in 1988 and became an international standard in 1990. A number of subsequent revisions have been made to the initial specification. The most recent revision is referred to as IEEE Std. 1003.1-2001 is actually a single common revision to IEEE Std 1003.1-1996, IEEE Std 1003.2-1992, and the Base Specifications of The Open Group Single UNIX Specification, Version 2.
Throughout the remainder of this document the use of the word POSIX will refer to IEEE Std. 1003.1-2001 unless otherwise specified.
The POSIX specification requires that the system header file unistd.h define the symbolic constant _POSIX_VERSION where the given value indicates the version of IEEE Std 1003.1 C-language binding segment to which the implementation conforms.
For the 1990 revision compliance the defined value of _POSIX_VERSION should be 1.
For the 1995 revision compliance the defined value of _POSIX_VERSION should be 199506L.
For the 2001 revision compliance the defined value of _POSIX_VERSION should be 200112L.
In addition to _POSIX_VERSION, it is also possible to use the sysconf( ) function programmatically at runtime and the _SC_VERSION name value to determine compliance as is shown below.
#define _POSIX_C_SOURCE 200112L
#include <unistd.h>
#include <stdio.h>
int main( int argv, char ** argc )
{
long x = sysconf(_SC_VERSION);
printf("%li\n", x );
return 0;
}
There are a number of systems which define _POSIX_VERSION and the return value of sysconf( _SC_VERSION ) to a seemingly random number, typically greater than 200112L. While this allows you to discover if a system supplies POSIX it does nothing to help indicate what level / revision it supports.
Unfortunately, there is no truly easy way to solve this problem.
To indicate that your code wants only the POSIX interfaces exposed, and thus assuring that it is POSIX "compliant", you should include the line in your header file(s):
#define _POSIX_C_SOURCE 1 /* For 1990 revision compliance */
#define _POSIX_C_SOURCE 199506L /* For 1995 revision compliance */
#define _POSIX_C_SOURCE 200112L /* For 2001 revision compliance */
There are a few items to note about the above #define:
* The define statement should be placed in a location within your source code where it will be processed PRIOR to any include directives. If you do not do this, the compiler and the prototypes from the system headers will normally just go to their default condition which is include everything.
* Even though many systems are still at the 1995 revision level, the headers are normally well built enough to handle the use of the new value by checking for a value greater than or equal to (as opposed to just equal to) 199506L; that value being the one for the 1995 revision.
* The given define is the current standard but in older revisions of the spec, the required define was given as _POSIX_SOURCE 1 which means if you see things getting exposed to your code that should not be there, you may want to revert to see if that helps.