I'm trying to build an old linux driver in my system (Kubuntu 21.10) with make -C ....
The driver is declaring static struct timeval frame_timestamp;
If I try to build it with something like
make -C /lib/modules/`uname -r`/build M=`pwd` V=1 modulesI'm getting ... has an incomplete type ‘struct timeval’ error and some other related errors.
So I believe I must include sys/time.h with #include <sys/time.h> into the code.
But in this case I'm getting fatal error: sys/time.h: No such file or directory. And I cant't find a way to resolve this. (The code already had some includes but none from sys/, they are all from linux/ and media/)
I ended up with a following Makefile:
obj-m := mydriver.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default: $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) M=$(PWD) V=1 modulesThe KDIR resolves into /lib/modules/5.13.0-21-generic/build which links to /usr/src/linux-headers-5.13.0-21-generic. There is no sys/time.h.
locate sys/time.h returns:
/usr/include/i386-linux-gnu/sys/time.h
/usr/include/sys/time.h
/usr/include/x86_64-linux-gnu/sys/time.hwhich belongs to libc6-dev and libc6-dev-i386:.dpkg -l *5.13.0-21* gives:
ii linux-headers-5.13.0-21 5.13.0-21.21 all Header files related to Linux kernel version 5.13.0
ii linux-headers-5.13.0-21-generic 5.13.0-21.21 amd64 Linux kernel headers for version 5.13.0 on 64 bit x86 SMP
ii linux-image-5.13.0-21-generic 5.13.0-21.21 amd64 Signed kernel image generic
un linux-image-unsigned-5.13.0-21-generic <нет> <нет> (описание недоступно)
ii linux-modules-5.13.0-21-generic 5.13.0-21.21 amd64 Linux kernel extra modules for version 5.13.0 on 64 bit x86 >
ii linux-modules-extra-5.13.0-21-generic 5.13.0-21.21 amd64 Linux kernel extra modules for version 5.13.0 on 64 bit x86I was trying to hardcode -I /usr/include/x86_64-linux-gnu/ into make -C - this has no effect.
I also tried to download a vanilla kernel from kernel.org:
and hardcode it as KDIR - that doesn't help and it's not containing sys/time.h either.
The question is: how to force my module build process to see the sys/time.h?
Does linux module allowed to link to libc? If it's not a part of the kernel, how to make it available for module buildprocess?
1 Answer
Ok, it seems one can't use sys/time.h in kernel module.
I have taken a look at definition of struct v4l2_buffer in linux/videodev2.h where the struct timeval is supposed to be used by the driver to initialize its timestamp field and found there:
truct v4l2_buffer { __u32 index; __u32 type; __u32 bytesused; __u32 flags; __u32 field;
#ifdef __KERNEL__ struct __kernel_v4l2_timeval timestamp;
#else struct timeval timestamp;
#endif struct v4l2_timecode timecode;
....The __KERNEL__ is defined automatically somewhere in make -C process, so up-to-date kernel expects to get __kernel_v4l2_timeval. Replacing struct timeval with struct __kernel_v4l2_timeval among with some other tricks allowed me to compile this old driver.