首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将设备驱动程序编译到内核

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

Stack Overflow用户
提问于 2017-12-09 09:23:44
回答 1查看 896关注 0票数 0

我正在尝试编译一个设备驱动程序,但是我得到了下面的错误,以及下面所有标题的相同错误

代码语言:javascript
复制
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编译程序:

代码语言:javascript
复制
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代码:

代码语言:javascript
复制
#include <stdio.h>
#include <stdli.h>

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

用命令

gcc试验.c -o试验

一切都在汇编。我怀疑这是带有内核头的东西,但我已经下载了指定的所有头。我在运行lubuntu 17.10

我是不是遗漏了什么?

非常感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-09 09:50:20

stdio.h是用户空间头文件,而不是内核空间,这就是make失败的原因。在驱动程序中,为什么我们包含所有的headers,因为bcz没有main()函数,对吗?

当你要做make时,观察你的makefile

代码语言:javascript
复制
 obj-m := message_slot.o 
 KDIR := /lib/modules/$(shell uname -r)/build 

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

如:

代码语言:javascript
复制
   #include <linux/stat.h> 

代码语言:javascript
复制
#include <stat.h>

代码语言:javascript
复制
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编译。

希望能帮上忙。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47727135

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档