Home > Software design >  Are there scenarios in which defining _POSIX_C_SOURCE can have negative side-effects?
Are there scenarios in which defining _POSIX_C_SOURCE can have negative side-effects?

Time:10-26

I set out to make my program C99, and so I compile it with the -std=c99 gcc flag. To make this work, because usleep() is deprecated, I have to use nanosleep(), which is sorta kinda not really part of C99, and requires you to define _POSIX_C_SOURCE >= 199309L.

Now, I am confused about how this affects my program and the compilation, and if it can have weird effects on different distros.

Mainly, my question is if I can leave it like that and trust it to work ok most of the time and not have any weird side-effects, or should I remove it and compile the program as C11?

CodePudding user response:

Are there scenarios in which defining _POSIX_C_SOURCE can have negative side-effects?

Defining _POSIX_C_SOURCE before including (otherwise) standard C library headers causes the headers to declare and define things that are specified by POSIX, in addition to standard C library things they declare and define.

If your program uses any of those POSIX things in a non-POSIX way, it can break. For example, a C program that does not use POSIX is free to declare nanosleep in its own way for its own purposes, and including the standard C library headers with that should not cause any problems. However, when POSIX features are requested, there may be conflicts between the POSIX declaration of nanosleep and the program’s declaration.

  •  Tags:  
  • c
  • Related