Search code examples
clinuxinclude-path

How do I include Linux header files like linux/getcpu.h?


I'm using Linux 3.5.0-17-generic #28-Ubuntu SMP Tue Oct 9 19:31:23 UTC 2012 x86_64 GNU/Linux, and I need to #include<linux/getcpu.h>. The compiler complains that it cannot find the file. Where are the header files for linux?


Solution

  • Short answer: usually, you don't include those headers directly.

    Most OS/Machine specific headers in there are automatically included for you by a more general header. Those that are not are linux only features which may or may not be available for the version you are running.

    As to getcpu, there is a more standardised version called sched_getcpu which is found in sched.h and has the same function.

    Alternatively you can test wether that system call is available on your system and call it manually:

    #define _GNU_SOURCE  
    #include <unistd.h>
    #include <sys/syscall.h>
    
    static inline int getcpu() {
        #ifdef SYS_getcpu
        int cpu, status;
        status = syscall(SYS_getcpu, &cpu, NULL, NULL);
        return (status == -1) ? status : cpu;
        #else
        return -1; // unavailable
        #endif
    }
    

    The variable errno (#include <errno.h>) gives the error code, if syscall returns -1.