简体   繁体   中英

‘getnstimeofday' is an implicit declaration in system call when <linux/time.h> 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.

PS compiling kernel V4.14.25 in Ubuntu 16.04

Since kernel version 3.17.x, getnstimeofday is no longer declared by #include <linux/time.h> . The solution is to add:

#include <linux/ktime.h>

Depending on the kernel version, #include <linux/ktime.h> will pull in the declaration of getnstimeofday from either <linux/time.h> (prior to 3.17.x) or from <linux/timekeeping.> (for 3.17.x onwards). There is no need to include <linux/timekeeping.h> directly.

Note that <linux/ktime.h> has been available since Linux kernel 2.6.16 onwards.

You may be able to remove your #include <linux/time.h> if there is nothing else in there you need to use. Test that by removing the line and building your code for any kernel 3.17.x or later.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM