kernel module build fails: sys/types.h: No such fi

2019-08-12 03:41发布

问题:

I'm unable to build a kernel module due to a missing .h file. I'm building the module on Ubuntu 14.04.

This is the make file I use:

$ cat Makefile 
obj-m += my_module.o

all:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules

clean:
    make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

And this is the output of 'make':

$ make
make -C /lib/modules/3.13.0-34-generic/build M=/home/user/location modules
make[1]: Entering directory `/usr/src/linux-headers-3.13.0-34-generic'
  CC [M]  /home/user/location/my_module.o
/home/user/location/my_module.c:2:23: fatal error: sys/types.h: No such file or directory
 #include <sys/types.h>
                       ^
compilation terminated.

The file sys/types.h exists:

/usr/include/x86_64-linux-gnu/sys/types.h

And a test program which includes it compiles:

$ cat test.c 
#include <sys/types.h>
#include <stdio.h>

int main()
{
    printf ("test\n");
    return 0;
}
$ gcc -o test test.c 
$ 

I looked at several posts and made sure I have libc6-dev and build-essential installed.

Any ideas?

回答1:

When you are developing a Linux kernel module, you should forget about the comforts of Standard C library(Glibc).Instead , you will have to use the Kernel C. So , you should write

#include <linux/types.h>

It will solve the problem.