is included,c,linux-kernel,system-calls,gettimeofday"/>

首页 > 解决方案 > ‘getnstimeofday' is an implicit declaration in system call when is included

问题描述

Anwser

Solved!! Thanks to @IanAbbott

the header should be:

#include <linux/ktime.h>
#include <linux/timekeeping.h>

rather than <linux/time.h>.

More detail see discussion.


Original Question

I am writing a system call names sys_my_time.c, which will use getnstimeofday(). I have imported <linux/time.h>. The code like this:

#include <linux/kernel.h>
#include <linux/linkage.h>
#include <linux/time.h>

asmlinkage int sys_my_time() {
  struct timespec t;
  getnstimeofday(&t);
  // ...
  return 0;
}

But while compiling, the error shows:

CC      kernel/sys_my_time.o
kernel/sys_my_time.c: In function ‘sys_my_time’:
kernel/sys_my_time.c:8:3: error: implicit declaration of function ‘getnstimeofday’ [-Werror=implicit-function-declaration]
getnstimeofday(&t);
^
cc1: some warnings being treated as errors
scripts/Makefile.build:320: recipe for target 'kernel/sys_my_time.o' failed
make[1]: *** [kernel/sys_my_time.o] Error 1
Makefile:1029: recipe for target 'kernel' failed
make: *** [kernel] Error 2

I have no idea why the error happens.

P.S. compiling kernel V4.14.25 in Ubuntu 16.04

标签: clinux-kernelsystem-callsgettimeofday

解决方案


由于内核版本 3.17.x,getnstimeofday不再由#include <linux/time.h>. 解决方案是添加:

#include <linux/ktime.h>

根据内核版本,#include <linux/ktime.h>将拉入getnstimeofdayfrom <linux/time.h>(3.17.x 之前)或 from <linux/timekeeping.h>(3.17.x 以后)的声明。不需要<linux/timekeeping.h>直接包含。

请注意,<linux/ktime.h>从 Linux 内核 2.6.16 开始就可以使用了。

#include <linux/time.h>如果那里没有其他需要使用的东西,您可以删除您的。通过删除该行并为任何内核 3.17.x 或更高版本构建代码来测试它。


推荐阅读