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=2012;

int hello_init()
{
    printk(KERN_ALERT "Hello kernel, it's %d!
",year);
    return 0;
}


void hello_exit()
{
    printk(KERN_ALERT "Bye, kernel!
");
}

// 下面两个为关键的模块函数
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

clip_image002

3、 insmod hello.ko

4、 dmesg

clip_image001

原文地址:https://www.cnblogs.com/smbx-ztbz/p/4769517.html