[英]implicit declaration of function ‘sched_setaffinity’
我正在编写一个需要在单核上运行的程序。 要将它绑定到单核,我使用的是sched_setaffinity()
,但编译器会发出警告:
implicit declaration of function 'sched_setaffinity'
我的测试代码是:
#include <stdio.h>
#include <unistd.h>
#define _GNU_SOURCE
#include <sched.h>
int main()
{
unsigned long cpuMask = 2;
sched_setaffinity(0, sizeof(cpuMask), &cpuMask);
printf("Hello world");
//some other function calls
}
能帮我解决一下吗? 实际上代码是经过编译和运行的,但我不确定它是在单核上运行还是在交换内核。
我使用的是Ubuntu 15.10和gcc 5.2.1版
您需要将#define _GNU_SOURCE
移到顶部。 在man sched_setaffinity
它说:
#define _GNU_SOURCE /* See feature_test_macros(7) */
而在man 7 feature_test_macros
它说:
注意:为了有效,必须在包含任何头文件之前定义功能测试宏。 这可以在编译命令(cc -DMACRO = value)中完成,也可以在包含任何头之前在源代码中定义宏。
因此,在一天结束时,您的代码应如下所示:
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <sched.h>
int main()
{
unsigned long cpuMask = 2;
sched_setaffinity(0, sizeof(cpuMask), &cpuMask);
printf("Hello world");
//some other function calls
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.