36

I want to use CPU_SET, which is a glibc linux-specific macro that should be defined in sched.h The manpage clearly states that _GNU_SOURCE must be defined so that the macro is defined. However, looking at the header, CPU_SET is defined only if __USE_GNU is defined (there is an #ifdef guard). I seem to remember a few years ago that _GNU_SOURCE was needed.

Questions:

1) Clearly the manpage is off. How do I notify the maintainer that the manpage is incorrect?

2) When did the transition from _GNU_SOURCE to __USE_GNU happen (either in terms of version or time)

3) Are there circumstances where newer versions of glibc still use _GNU_SOURCE? Or can I safely assume that defining __USE_GNU is sufficient?

Foo Bah
  • 25,660
  • 5
  • 55
  • 79

2 Answers2

47

_GNU_SOURCE is the only one you should ever define yourself. __USE_GNU is defined internally through a mechanism in features.h (which is included by all other glibc headers) when _GNU_SOURCE is defined, and possibly under other conditions. Defining or undefining __USE_GNU yourself will badly break the glibc headers.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • I still get errors if I use `_GNU_SOURCE` only. Any other ideas as to why it could happen? – Foo Bah Sep 04 '11 at 02:27
  • 14
    All feature test macros such as `_GNU_SOURCE` **must** be defined before **any** standard header is included. You can't wait to define it until just before the header you need it for. – R.. GitHub STOP HELPING ICE Sep 04 '11 at 02:28
  • 1
    I ended up having to define `_GNU_SOURCE` at the top line of the most inner header file, but it worked :) – Foo Bah Sep 04 '11 at 02:34
  • 11
    It really should be at the top line of your source file, or even better, on the command line to the compiler in the form `-D_GNU_SOURCE`. Then there's no doubt that it's defined from the beginning. – R.. GitHub STOP HELPING ICE Sep 04 '11 at 02:51
  • On an older SPARC platform, I had to manually `#include ` to get this to work without expliciting defining `__USE_GNU`. – marshall.ward Jun 19 '17 at 12:44
14

you have to define_GNU_SOURCE before anything else. This snippet works here:

#define _GNU_SOURCE
#include <sched.h>


int main()
{
    cpu_set_t set;
    CPU_SET(0, &set);
    return 0;
}
Giuseppe Scrivano
  • 1,385
  • 10
  • 13