【问题标题】:Compiling device drivers to the kernel将设备驱动程序编译到内核
【发布时间】:2018-05-23 10:53:16
【问题描述】:

我正在尝试编译设备驱动程序,但出现以下错误, 并且所有后续标题都相同

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 测试
一切都编译。 我怀疑它与内核头文件有关,但我已经按照指定下载了所有头文件。我正在运行 lubuntu 17.10
我错过了什么吗?
非常感谢

【问题讨论】:

标签: c linux linux-kernel linux-device-driver


【解决方案1】:

stdio.h 是用户空间头文件而不是内核空间,这就是您的 make 失败的原因。 在驱动程序中为什么我们包含所有headers,因为它没有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 进行编译。

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2015-06-01
    • 2012-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-29
    • 2017-03-02
    • 2011-02-24
    • 1970-01-01
    相关资源
    最近更新 更多