第一步,我们通过在内核中添加drivers/new/test.c,代码如下:
#include <linux/init.h>
#include <linux/module.h>
int __init test_init(void)
{
printk("HELLO KERNEL\n");
return 0;
}
void __exit test_exit(void)
{
printk("BYE KERNEL\n");
}
MODULE_LICENSE("GPL");
module_init(test_init);
module_exit(test_exit);
第二步,修改drivers文件夹下的Makefile,添加obj-y += new/,如下图所示:
![](https://img-blog.csdnimg.cn/img_convert/0c2928fcc06168030c613cebbb819dd5.png)
并且,在new文件夹下面建立Makefile文件,代码如下:
obj-m += test.o
KERNEL=~/Code/baiwei/code/kernel/linux/linux-5.10
all:
make -C $(KERNEL) M=`pwd` modules
clean:
make -C $(KERNEL) M=`pwd` modules clean
第三步,在new目录在输入make命令,结果报错了,错误截图如下:
![](https://img-blog.csdnimg.cn/img_convert/abbd8059300e44766e9ff99d7ada2ee9.png)
主要的报错信息是No rule to make target 'scripts/module.lds'(上图是在linux-5.10目录下执行make命令,没关系的),这个报错信息让我花费一天时间找原因,终于找到了解决方法就是在linux-5.10目录下执行make modules_prepare命令,如下图所示:
![](https://img-blog.csdnimg.cn/img_convert/3bfef0669d548e59fbf49d489d227780.png)
执行完后,上面问题就解决了。分享一下参考网址:
http://t.zoukankan.com/dream397-p-15405600.html
再补充一些问题:
make编译报下面的错误:
![](https://img-blog.csdnimg.cn/img_convert/2df383b1c578538f8965a7df143da34b.png)
可以使用make oldconfig命令,如下图所示:
![](https://img-blog.csdnimg.cn/img_convert/accb6d7794e518f4133642fc089b5522.png)
make编译报下面的错误:
![](https://img-blog.csdnimg.cn/img_convert/c9859c251488aa3d599d6211f442398a.png)
可以使用make prepare命令,如下图所示:
![](https://img-blog.csdnimg.cn/img_convert/d5021524373dc891677b307c80d63659.png)
3.make编译报下面的错误:
![](https://img-blog.csdnimg.cn/img_convert/0a454ae6a147f199ca7a8e8a7246d64e.png)
参考下面这个网址: