将设备驱动程序编译到内核

Compiling device drivers to the kernel

我正在尝试编译一个设备 driver,但出现以下错误, 和所有以下 header

ddd@ddd:~/Desktop$ make
make -C /lib/modules/4.13.0-19-generic/build  M=/home/ddd/Desktop  modules 
make[1]: Entering directory '/usr/src/linux-headers-4.13.0-19-generic'
  CC [M]  /home/ddd/Desktop/message_slot.o
/home/ddd/Desktop/message_slot.c:23:10: fatal error: stdio.h: No such file or directory
 #include <stdio.h>
          ^~~~~~~~~
compilation terminated.
scripts/Makefile.build:309: recipe for target '/home/ddd/Desktop/message_slot.o' failed
make[2]: *** [/home/ddd/Desktop/message_slot.o] Error 1
Makefile:1546: recipe for target '_module_/home/ddd/Desktop' failed
make[1]: *** [_module_/home/ddd/Desktop] Error 2
make[1]: Leaving directory '/usr/src/linux-headers-4.13.0-19-generic'
Makefile:5: recipe for target 'all' failed
make: *** [all] Error 2
ddd@ddd:~/Desktop$ 

我使用以下 makefile 编译程序:

obj-m := message_slot.o 
KDIR := /lib/modules/$(shell uname -r)/build 
PWD := $(shell pwd) 
all: 
    $(MAKE) -C $(KDIR) M=$(PWD) modules 
clean: 
    $(MAKE) -C $(KDIR) M=$(PWD) clean

问题是,通过 运行 小的 .c 代码:

#include <stdio.h>
#include <stdli.h>

int main(){
  printf("test");
 }

使用命令
gcc test.c -o 测试
一切编译。 我怀疑这是内核 headers 的问题,但我已经按照指定下载了所有 headers。我是 运行 lubuntu 17.10
我错过了什么吗?
非常感谢

stdio.h 是用户 space 头文件而不是内核 space,这就是您的 make 失败的原因。 在驱动程序中为什么我们要包括所有 headers 因为 bcz 它没有 main() 功能,对吗?

你什么时候会做 make,观察你的 makefile

 obj-m := message_slot.o 
 KDIR := /lib/modules/$(shell uname -r)/build 

这意味着您正在编译为模块,您的源代码将在 /usr/src/linux-4(某些版本)中。

例如

   #include <linux/stat.h> 

没有

#include <stat.h>

xyz@xyz-PC:/usr/src/linux-4.1.39/include/linux$ ls -l stdio.h  
        ls: cannot access stdio.h: No such file or directory

为什么要在您的驱动程序中包含 stdio.h 因为您不打算使用 printf,而是使用 printk()?

是的,在应用程序中您可以包含 stdio.h,因为您使用 gcc 编译器作为 file 而不是 module.

进行编译

希望对您有所帮助。