Sandeep
Sandeep

Reputation: 19502

Macros like _GNU_SOURCE, what do they mean?

Lot many times while referring to linux header files or man files, I see the following macros used..

Ex : man mkstemp

In this man page we can see that the below macros are featured.

_GNU_SOURCE
_BSD_SOURCE
_SVID_SOURCE
_XOPEN_SOURCE
_XOPEN_SOURCE_EXTENDED

What am I supposed to understand to write a correct program if I am using these API's/Headers?

Upvotes: 3

Views: 1363

Answers (1)

Read feature_test_macros(7) man page (and the §1.3.4 Feature Test Macros chapter of GNU libc documentation).

You might compile your whole program with some special feature symbols. For instance, I often compile a program with -D_GNU_SOURCE. This means that I want all the extra GNU specific features provided on my system by GNU libc etc. You could instead compile with -D_POSIX_C_SOURCE=200112L if you want strict POSIX 2001 compliance (and nothing more).

Alternatively, if all your .c files are just #include-ing only your own header, that header could start with #define _GNU_SOURCE 1 followed by several system #include ....

The point is that a GNU/Linux system obey to several standards (with GNU providing its own standard), and you might choose which ones.

GNU libc (which is the most common libc available on Linux, but you could use some other libc, like musl-libc ....) provides a lot of functions, features and headers not available on other systems, e.g. <argp.h> (header), fopencookie (function), %m format control directive in printf feature.

It is also relevant if you intend to code a program portable to other POSIX systems (e.g. to MacOSX). On MacOSX or AIX systems you don't have getopt_long since it is a GNU specific function.

Upvotes: 3

Related Questions