Defining feature test macros
In this recipe, we'll learn what some common POSIX standards are, how and why to use them, and how we specify them using feature test macros.
We have already seen several examples of when we have included either a POSIX standard or some specific C standard. For example, when we used getopt()
, we defined _XOPEN_SOURCE 500
at the very top of the source code file (mph-to-kph_v2.c
from Chapter 2, Making Your Programs Easy to Script).
A feature test macro controls the definitions that are exposed by system header files. We can leverage this in two ways. Either we can use it to create portable applications by using a feature test macro that prevents us from using non-standard definitions or we can use it the other way around, allowing us to use non-standard definitions.
Getting ready
We will write two small programs in this recipe, str-posix.c
and which-c.c
. You can either download them from https://github.com/PacktPublishing/Linux-System-Programming...