Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does -D_XOPEN_SOURCE do/mean?

Tags:

c

gcc

I recently encountered some code that gcc would not compile without this arg. I checked the gcc man page, but did not find this specific option. I did find XOPEN_SOURCE, but there was little explanation of what it does.

Can someone please elaborate? I know -D_XOPEN_SOURCE can be set to different values, such 400, 600, but what do those do?

like image 210
anio Avatar asked Mar 21 '11 14:03

anio


People also ask

What is _xopen_source?

When you do #define _XOPEN_SOURCE <some number> or cc -D_XOPEN_SOURCE=<some number> it tells your compiler to include definitions for some extra functions that are defined in the X/Open and POSIX standards.

What is #define _gnu_source?

If you define _GNU_SOURCE , you will get: access to lots of nonstandard GNU/Linux extension functions. access to traditional functions which were omitted from the POSIX standard (often for good reason, such as being replaced with better alternatives, or being tied to particular legacy implementations)

What is feature test macro?

DESCRIPTION top. Feature test macros allow the programmer to control the definitions that are exposed by system header files when a program is compiled. NOTE: In order to be effective, a feature test macro must be defined before including any header files.


1 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 extra functions that are defined in the X/Open and POSIX standards.

This will give you some extra functionality that exists on most recent UNIX/BSD/Linux systems, but probably doesn't exist on other systems such as Windows.

The numbers refer to different versions of the standard.

  • 500 - X/Open 5, incorporating POSIX 1995
  • 600 - X/Open 6, incorporating POSIX 2004
  • 700 - X/Open 7, incorporating POSIX 2008

You can tell which one you need (if any) by looking at the man page for each function 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 

Which means that you should put one of these:

#define _SVID_SOURCE #define _BSD_SOURCE #define _XOPEN_SOURCE 500 #define _XOPEN_SOURCE 600 #define _XOPEN_SOURCE 700 

at the top of your source file before doing any #includes if you want to use strdup.

Or you could put

#define _GNU_SOURCE 

there instead, which enables all functionality, with the downside that it might not compile on Solaris, FreeBSD, Mac OS X, etc.

It's a good idea to check each man page before doing a #include, #define, or using a new function, because sometimes their behavior changes depending on what options and #defines you have, for example with basename(3).

See also:

  • Linux: gcc with -std=c99 complains about not knowing struct timespec
  • glibc feature test macros
  • The Compilation Environment - Open Group Base Specification issue 6 (a.k.a. X/Open 6)
  • POSIX - Wikipedia
  • Single UNIX Specification - Wikipedia
like image 149
Mikel Avatar answered Oct 04 '22 00:10

Mikel