1.hello.c
#include <linux/init.h>
#include <linux/module.h>
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);
2.Makefile, 注意M为大写,否则会报错
obj-m := hello.o
KERNELDIR := /lib/modules/2.6.35-22-generic/build
PWD := $(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
......
Jul 21 23:38:24 ubuntu kernel: [14829.165760] Hello, world
Jul 21 23:40:02 ubuntu kernel: [14927.159492] Goodbye, cruel world
#include <linux/init.h>
#include <linux/module.h>
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);
2.Makefile, 注意M为大写,否则会报错
obj-m := hello.o
KERNELDIR := /lib/modules/2.6.35-22-generic/build
PWD := $(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
3.测试
gavin@ubuntu:~/linux_驱动开发$ lsmod
Module Size Used by
hello 692 0
gavin@ubuntu:~/linux_驱动开发$ sudo insmod ./hello.ko
gavin@ubuntu:~/linux_驱动开发$ sudo rmmod hello
......
Jul 21 23:38:24 ubuntu kernel: [14829.165760] Hello, world
Jul 21 23:40:02 ubuntu kernel: [14927.159492] Goodbye, cruel world