System Header Files

The system header files quite often a mish-mash of functions in them. For instance, stdio.h on my system has functions such as:

For portability, we want to use as restricted a set of functions (and macros) that we can get away with, preferably the ANSI subset.

On most systems, there is a strict ANSI mode compilation flag that will restrict the namespace of functions defined in headers to the ANSI subset.

However, we may need to use a function from some other standard. Usually, we can achieve this by defining a feature test macro; i.e. #define some macro variable. For example, by defining _POSIX_C_SOURCE to 1, we get access to the POSIX.1 set of functions.

I like to know where I have used an expanded name-space; further, I want to restrict the name-space expansion to as few files as possible. For instance, I may want to use the POSIX.1 version of stdio.h, but keep the ANSI version of stdlib.h. So, what I do is create a file posix_stdio.h with contents:


	#ifndef H_posix_stdio
	#define H_posix_stdio

	#ifndef _POSIX_C_SOURCE
	  #define _POSIX_C_SOURCE	1
	  #include <stdio.h>
	  #undef _POSIX_C_SOURCE
	#else
	  #include <stdio.h>
	#endif

	#endif

Now, when I want to use a POSIX.1 extension from stdio.h, I use "#include "posix_stdio.h"" instead of "#include <stdio.h>".

Note: There may be some systems in which, if you compile one module using _POSIX_C_SOURCE, you will have to compile all modules with the same feature set. Or, if you use the POSIX.? namespace for some header, then you will have to use that namespace every time you include the header. While I have not run into such a situation, its feasible that it could occur.


There is no standard ANSI feature test macro. In general, you should start off by restricting yourself to the ANSI subset and expanding your namespace on demand. However, you may find yourself in a situation where you can't do this. This usually occurs when maintaining old code.

Fortunately, there is usually some (system-dependent) ANSI feature test macro. It should be easy to find by looking at one of the standard header files. It is usually one of:

If so, you can define ANSI versions of standard header files such as ansi_stdio.h just as above.


Next Prev Main Top Feedback