Today I had to use the basename()
function, and the man 3 basename
(here) gave me some strange message:
Notes
There are two different versions of basename() - the POSIX version described above, and the GNU version, which one gets after
#define _GNU_SOURCE
#include <string.h>
I'm wondering what this #define _GNU_SOURCE
means: is it tainting the code I write with a GNU-related license? Or is it simply used to tell the compiler something like "Well, I know, this set of functions is not POSIX, thus not portable, but I'd like to use it anyway".
If so, why not give people different headers, instead of having to define some obscure macro to get one function implementation or the other?
Something also bugs me: how does the compiler know which function implementation to link with the executable? Does it use this #define
as well?
Anybody have some pointers to give me?
Defining _GNU_SOURCE
has nothing to do with license and everything to do with writing (non-)portable code. If you define _GNU_SOURCE
, you will get:
mount
, ifconfig
, etc.As long as you're aware of these things, it should not be a problem to define _GNU_SOURCE
, but you should avoid defining it and instead define _POSIX_C_SOURCE=200809L
or _XOPEN_SOURCE=700
when possible to ensure that your programs are portable.
In particular, the things from _GNU_SOURCE
that you should never use are #2 and #4 above.