When you do
#define _XOPEN_SOURCE <some number>
or
cc -D_XOPEN_SOURCE=<some number>
it tells your compiler to include definitions for some additional functions that are defined in the X / Open and POSIX standards.
This will give you additional functionality that exists on the latest UNIX / BSD / Linux systems, but probably does not exist on other systems such as Windows.
Numbers refer to different versions of the standard.
You can specify which one you need (if any) by looking at the man page for each function that you call.
For example, man strdup says:
Feature Test Macro Requirements for glibc (see feature_test_macros(7)): strdup(): _SVID_SOURCE || _BSD_SOURCE || _XOPEN_SOURCE >= 500 strndup(), strdupa(), strndupa(): _GNU_SOURCE
This means that you must put one of them:
#define _SVID_SOURCE #define _BSD_SOURCE #define _XOPEN_SOURCE 500 #define _XOPEN_SOURCE 600 #define _XOPEN_SOURCE 700
at the top of the source file before making #include if you want to use strdup .
Or you can put
#define _GNU_SOURCE
instead, which allows you to use all the functionality that might not compile on Solaris, FreeBSD, Mac OS X, etc.
It is recommended to check each man page before doing #include , #define or using a new function, because sometimes their behavior changes depending on what parameters and #define you have, for example, with basename (3) .
See also:
Mikel Apr 20 2018-11-11T00: 00Z
source share