如何单独编译内核模块
1. hello.c
#include <linux/init.h> #include <linux/module.h> MODULE_LICENSE("Dual BSD/GPL"); static int hello_init(void) { printk(KERN_ALERT "Hello World enter "); return 0; } static void hello_exit(void) { printk(KERN_ALERT "Hello World exit "); } module_init(hello_init); module_exit(hello_exit); MODULE_AUTHOR("Song YanNa"); MODULE_DESCRIPTION("A Sample Hello World Module"); MODULE_ALIAS("A Sample module");
2. Makefile
# #Makefile for the helloworld.c # obj-m := hello.o CURRENT_PATH := $(shell pwd)
LINUX_KERNEL := $(shell uname -r) LINUX_KERNEL_PATH := /usr/src/linux-headers-$(LINUX_KERNEL) all: $(MAKE) -C $(LINUX_KERNEL_PATH) M=$(CURRENT_PATH) modules clean: rm *.ko rm *.o
3.在shell终端输入make
出现如下信息,说明编译成功。
NaNa:/home/nana/driver # make make -C /home/nana/driver/linux-3.1.10 M=/home/nana/driver modules make[1]: Entering directory `/home/nana/driver/linux-3.1.10' CC [M] /home/nana/driver/hello.o Building modules, stage 2. MODPOST 1 modules CC /home/nana/driver/hello.mod.o LD [M] /home/nana/driver/hello.ko make[1]: Leaving directory `/home/nana/driver/linux-3.1.10'
4. 将模块载入内核
sudo insmod ./hello.ko
5. 删除内核中的模块
rmmod hello.ko
在终端输入dmesg,可以查看内核的日志信息
查询安装的模块
lsmod