0

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?

devgirl05
  • 173
  • 7
  • 3
    If you have no specific reason to be C99 compatible, I would aim for at least C11 (or later) at this point. – sj95126 Oct 24 '22 at 16:09
  • 1
    Does https://stackoverflow.com/questions/68259832/why-do-we-need-feature-test-macros answer your question? – KamilCuk Oct 24 '22 at 16:27
  • 1
    `usleep()` isn't a part of C99, or any other version of Standard C, either. Using `_POSIX_C_SOURCE` means that extensions not defined by POSIX should not be available unless you explicitly enable them somehow. (I usually use `#define _XOPEN_SOURCE 700` if only because the numbers are easier to remember, but I also use a header to set it. And I usually compile with either `-std=c99` or `-std=c11` — using the older version where I haven't updated my `makefile` yet.). If you want certain GNU extensions, you'll have to enable them with, for example, `#define _GNU_SOURCE` or something similar. – Jonathan Leffler Oct 24 '22 at 16:44
  • 1
    Functions that ask the program to *sleep* for a specified amount of time aren't part of the standard because the C language is not interested in policing exactly how an operating systems chooses to schedule its processes. `#define`ing `_POSIX_C_SOURCE` is going to limit the portability of your program, because it will only work on `POSIX`-based operating systems, but `sleep()` is part of the `POSIX` standard so unless you wish to use this program on another operating system someday or have some namespace collisions with the other `POSIX` macros/functions/types you'll be fine – Willis Hershey Oct 24 '22 at 18:08

1 Answers1

0

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.

Willis Hershey
  • 1,520
  • 4
  • 22
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312