如何包含Linux头文件,例如linux/getcpu.h? [英] How do I include Linux header files like linux/getcpu.h?

查看:73
本文介绍了如何包含Linux头文件,例如linux/getcpu.h?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Linux 3.5.0-17-generic#28-Ubuntu SMP Tue Oct 9 19:31:23 UTC 2012 x86_64 GNU/Linux ,我需要#包括< linux/getcpu.h> .编译器抱怨找不到文件.linux的头文件在哪里?

解决方案

简短的回答:通常,您不直接包含这些标头.

其中的大多数OS/Machine特定标头都会自动包含在更通用的标头中.那些不是仅Linux功能,可能对于您正在运行的版本可用,也可能不可用.

关于 getcpu ,还有一个更标准化的版本,称为 sched_getcpu ,可在 sched.h 中找到,并且具有相同的功能./p>

或者,您还可以测试系统调用在您的系统上是否可用,然后手动调用它:

  #define _GNU_SOURCE#include< unistd.h>#include< sys/syscall.h>静态内联int getcpu(){#ifdef SYS_getcpuint cpu,状态;状态=系统调用(SYS_getcpu,& cpu,NULL,NULL);返回(状态== -1)?状态:cpu;#别的返回-1;//不可用#万一} 

如果syscall返回-1,则变量errno( #include< errno.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?

解决方案

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.

这篇关于如何包含Linux头文件,例如linux/getcpu.h?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆