简体   繁体   English

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

[英]Compiling device drivers to the kernel

I'm trying to compile a device driver, but i get the following error, and the same one for all following 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$ 

I compiler the program using the following makefile: 我使用以下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

The thing is, that by running the small .c code: 事实是,通过运行小的.c代码:

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

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

with the command 用命令
gcc test.c -o test gcc test.c -o测试
everything compiles. 一切都会编译。 I suspect it's something with the kernel headers, but i've downloaded all headers as specified. 我怀疑它与内核头文件有关,但我已按照指定下载了所有头文件。 I'm running lubuntu 17.10 我正在运行lubuntu 17.10
am i missing something? 我想念什么吗?
Thanks a lot 非常感谢

stdio.h is user space header file not kernel space, that's why your make fails. stdio.h是用户空间头文件而不是内核空间,这就是为什么make失败的原因。 In driver program why we are including all headers because bcz its not having main() function, Right ? 在驱动程序中,为什么要包括所有headers因为bcz没有main()函数,对吗?

when you will do make , observe your makefile 当你做make ,观察你的makefile

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

that means you are compiling as a modules and your source code will be in /usr/src/linux-4 (some version). 这意味着您将作为模块进行编译,并且源代码将位于/usr/src/linux-4 (某些版本)中。

for eg 例如

   #include <linux/stat.h> 

not

#include <stat.h>

and

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

In your driver program why are you including stdio.h because you are not going to use printf, instead printk() ? 在驱动程序中,为什么要包括stdio.h,因为您将不使用printf,而是使用printk()?

Yes in application program you can include stdio.h because you compiling using gcc compiler as a file not as a module . 是的,在应用程序中可以包含stdio.h因为使用gcc编译器作为file而不是作为module进行编译。

I hope it helps. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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