(編譯後的報錯/警告記錄)linux/module.h: 沒有那個文件或目錄/linux/module.h: No such file or directory

(轉自,侵刪)https://www.cnblogs.com/zhangjy6/p/5462644.html

缺少linux kernel 頭文件

sudo apt-get install linux-headers-$(uname -r)

其中uname -r是顯示系統信息

要在Ubuntu中安裝整個Linux內核源代碼

sudo apt-get install linux-source

一個簡單的內核模塊

#include <linux/init.h>
#include <linux/module.h>
 
MODULE_LICENSE("Dual BSD/GPL");
 
static int hello_init(void)
{
        printk(KERN_ALERT "Hello, world\n");
        return 0;
}
 
static void hello_exit(void)
{
        printk(KERN_ALERT "Goodbye, cruel world\n");
}
 
module_init(hello_init);
module_exit(hello_exit);

Makefile文件(注意M要大寫)

# at first type on ur terminal that $(uname -r) then u will get the version..
# that is using on ur system
 
obj-m += hello.o
 
KDIR =/usr/src/linux-headers-$(shell uname -r)
 
all:
        $(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
 
clean:
        rm -rf *.o *.ko *.mod.* *.symvers *.order

運行

$ sudo insmod hello.ko
$ dmesg           
$ sudo rmmod hello
$ dmesg

系統ubunttu16.04

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章