簡體   English   中英

將設備驅動程序編譯到內核

[英]Compiling device drivers to the kernel

我正在嘗試編譯設備驅動程序,但出現以下錯誤,並且所有以下標頭都出現了相同的錯誤

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
我想念什么嗎?
非常感謝

stdio.h是用戶空間頭文件而不是內核空間,這就是為什么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進行編譯。

希望對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM