(编译后的报错/警告记录)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

猜你喜欢

转载自blog.csdn.net/weixin_43387612/article/details/89364358