What does “#define _GNU_SOURCE” imply?

后端 未结 4 940
梦毁少年i
梦毁少年i 2020-11-27 09:53

Today I had to use the basename() function, and the man 3 basename (here) gave me some strange message:

Notes

4条回答
  •  天命终不由人
    2020-11-27 10:12

    For exact details on what are all enabled by _GNU_SOURCE, documentation can help.

    From the GNU documentation:

    Macro: _GNU_SOURCE

    If you define this macro, everything is included: ISO C89, ISO C99, POSIX.1, POSIX.2, BSD, SVID, X/Open, LFS, and GNU extensions. In the cases where POSIX.1 conflicts with BSD, the POSIX definitions take precedence.

    From the Linux man page on feature test macros:

    _GNU_SOURCE

    Defining this macro (with any value) implicitly defines _ATFILE_SOURCE, _LARGEFILE64_SOURCE, _ISOC99_SOURCE, _XOPEN_SOURCE_EXTENDED, _POSIX_SOURCE, _POSIX_C_SOURCE with the value 200809L (200112L in glibc versions before 2.10; 199506L in glibc versions before 2.5; 199309L in glibc ver‐ sions before 2.1) and _XOPEN_SOURCE with the value 700 (600 in glibc versions before 2.10; 500 in glibc versions before 2.2). In addition, various GNU-specific extensions are also exposed.

    Since glibc 2.19, defining _GNU_SOURCE also has the effect of implicitly defining _DEFAULT_SOURCE. In glibc versions before 2.20, defining _GNU_SOURCE also had the effect of implicitly defining _BSD_SOURCE and _SVID_SOURCE.

    Note: _GNU_SOURCE needs to be defined before including header files so that the respective headers enable the features. For example:

    #define _GNU_SOURCE
    
    #include 
    #include 
    ...
    

    _GNU_SOURCE can be also be enabled per compilation using -D flag:

    $ gcc -D_GNU_SOURCE file.c
    

    (-D is not specific to _GNU_SOURCE but any macro be defined this way).

提交回复
热议问题