13

What advantages does using #define _POSIX_SOURCE in C confer to my program? Can I access more libraries in my program or I can call some functions which are present directly in C. What is this macro used for?

MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
  • 1
    Related: https://stackoverflow.com/questions/18948661/what-does-the-flag-d-posix-c-source-200112l-mean/36264825#36264825 – Barmar Jan 19 '18 at 01:05

1 Answers1

24

It allows you to use functions that are not part of the standard C library but are part of the POSIX.1 (IEEE Standard 1003.1) standard. Using the macros described in feature_test_macros allows you to control the definitions exposed by the system header files.

As far as I know _POSIX_SOURCE is obsolete and you should use _POSIX_C_SOURCE instead.

For example, if you want to use strndup, you have to use

#define _POSIX_C_SOURCE 200809L

See also

MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
Pablo
  • 13,271
  • 4
  • 39
  • 59
  • 12
    If you are working on Linux with GCC, then by default it works in `-std=gnu11` or `-std=gnu90` mode, and the POSIX (and some other) sets of symbols are made visible without you needing to set `_XOPEN_SOURCE` or `_POSIX_C_SOURCE` or other similar macros. If you use `-std=c11` or `-std=c99` or (perish the thought) `-std=c90`, then POSIX and GNU symbols are not available by default; you have to define `_XOPEN_SOURCE` or `_POSIX_C_SOURCE` to see the POSIX symbols. – Jonathan Leffler Jan 19 '18 at 04:08