隐式函数声明'sched_setaffinity' [英] implicit declaration of function ‘sched_setaffinity’

查看:499
本文介绍了隐式函数声明'sched_setaffinity'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个需要在单核上运行的程序。为了将它绑定到单核上,我使用了 sched_setaffinity(),但编译器给出了警告:















$ b $ p $测试代码是:

  #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);
//一些其他函数调用
}

你能帮我想办法。实际上,代码是编译和运行的,但我不确定它是在单核上运行还是在切换核心。



我使用Ubuntu 15.10和gcc版本5.2 .1

解决方案

您需要将 #define _GNU_SOURCE 移至顶端。在 man sched_setaffinity 中表示:

  #define _GNU_SOURCE / *请参阅feature_test_macros (7)* / 

man 7 feature_test_macros 文件。这可以在
编译命令(cc -DMACRO = value)中完成,也可以在包含任何头文件之前在
中定义宏源代码。

因此,在一天结束时,您的代码应如下所示:

 # define _GNU_SOURCE 
#include
#include< unistd.h>
#include< sched.h>


int main()
{
unsigned long cpuMask = 2;
sched_setaffinity(0,sizeof(cpuMask),& cpuMask);
printf(Hello world);
//一些其他函数调用
}


I'm writing a program which needed to be run on single core. To bind it to single core, I'm using sched_setaffinity(), but the compiler gives warning:

implicit declaration of function ‘sched_setaffinity’

My test code is:

#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
}

Can you please help me to figure it out. Actually code is compiled and run, but I'm not sure whether it is running on single core or is switching cores.

I'm using Ubuntu 15.10 and gcc version 5.2.1

解决方案

You need to move #define _GNU_SOURCE to a top. In man sched_setaffinity it says:

 #define _GNU_SOURCE             /* See feature_test_macros(7) */

while in man 7 feature_test_macros it says:

NOTE: In order to be effective, a feature test macro must be defined before including any header files. This can be done either in the compilation command (cc -DMACRO=value) or by defining the macro within the source code before including any headers.

So at the end of the day your code should look like this:

#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
}

这篇关于隐式函数声明'sched_setaffinity'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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