What does -D_XOPEN_SOURCE do?

I recently came across some code that gcc would not compile without this argument. I checked the gcc man page but did not find this particular option. I found XOPEN_SOURCE , but there was little explanation of what it was doing.

Can anyone comment? I know that -D_XOPEN_SOURCE can be set to different values, such as 400 , 600 , but what do they do?

+78
c gcc
Mar 21 2018-11-11T00:
source share
4 answers

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:

+101
Apr 20 2018-11-11T00:
source share

-D is the c compiler option for defining a preprocessor variable. In this case, _XOPEN_SOURCE .

This does not affect the behavior of the compiler itself, but rather affects how some libraries, for example. c standard library behave. There are several options. In most cases, they refer to some standard document about a specific UNIX programming interface or a specific library provider.

Sometimes it is necessary to define one of them, since the behavior of some standard functions or even their signatures may differ between standards. Therefore, you may need to use -D_XOPEN_SOURCE or something similar to enable compatibility mode.

Another possible use of these flags is to make sure that your source code remains within a certain standard by including the extensions offered by your implementation of the C library. This is one of the measures you can use to make sure that your code runs at the maximum possible amount. platforms.

+7
Mar 21 2018-11-21T00:
source share

Thus, the title should belong to the definition of a particular norm, for example posix. The actual rate to which it belongs is determined by the value (here, for example, 400 or 600). See Link for binding to norms / values.

+3
Mar 21 2018-11-21T00:
source share

For some unknown reason, Mac OS / X (Xcode) requires 600 to define strdup (), even if it's in the 1995 specification. Mozilla and others ran into this ...

0
Mar 28 '16 at 15:47
source share



All Articles