16

I included:

#include <sched.h>

#define _GNU_SOURCE

Then in my code I have written (brief mention):

cpu_set_t set; 

CPU_ZERO(&set); 
CPU_SET(proc_num, &set); 
if (sched_setaffinity(gettid(), sizeof(cpu_set_t), &set))
{
    perror("sched_setaffinity");
    return NULL;
}

But when I compile I find

undefined reference to 'CPU_ZERO'

undefined reference to 'CPU_SET' 

How can I fix this problem?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
muskaan
  • 177
  • 2
  • 2
  • 11

1 Answers1

32

You need to place

#define _GNU_SOURCE

at least before

#include <sched.h>

as the define steers what the file included shall provide.

More on this on the related man-page here.


Update:

To make sure everything is set as needed, place the #define at the very beginning of your source files, that is before all #includes.

Alternatively you can pass the #define on GCC's command line by specifying the option

-D_GNU_SOURCE
alk
  • 69,737
  • 10
  • 105
  • 255
  • 6
    basically it worked after defining before all the library functions. – muskaan Jun 05 '14 at 07:33
  • @LinconFive: Not really. See this answer: https://stackoverflow.com/a/10687353/694576 or this one, being even more explicit: https://stackoverflow.com/a/7297011/694576 – alk Mar 05 '20 at 06:59