Ubuntu中编译helloworld驱动

时间:2023-03-09 04:00:12
Ubuntu中编译helloworld驱动

1、 新建hello文件夹

2、hello.c

#ifndef __KERNEL__
# define __KERNEL__
#endif
#ifndef MODULE
# define MODULE
#endif // 下面的是主要的内容
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h> MODULE_LICENSE("GPL"); static int year=; int hello_init()
{
printk(KERN_ALERT "Hello kernel, it's %d!\n",year);
return ;
} void hello_exit()
{
printk(KERN_ALERT "Bye, kernel!\n");
} // 下面两个为关键的模块函数
module_init(hello_init);
module_exit(hello_exit);

3、 Makefile

obj-m := hello.o

all :
$(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules clean:
$(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

注意:$(MAKE)前面为一个TAB键

4、 make

Ubuntu中编译helloworld驱动

3、 insmod hello.ko

4、 dmesg

Ubuntu中编译helloworld驱动