今天晚上配置Linux驱动开发环境,网络上的参考都有这样那样的问题,还是一句老话,做过的,才是自己的。下面是配置过程:

安装内核代码

sudo aptitude install linux-source-2.6.26

安装后去/usr/src目录下解压,然后进入linux-source-2.6.26目录,执行

make oldconfig

make prepare

make scripts

下面贴出hello world代码和makefile

ganquan@debian:~/Driver$ cat 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\n");
 
return 0;
 
}
 
static void hello_exit(void)
 
{
 
printk(KERN_ALERT "Good bye cruel world\n");
 
}
 
module_init(hello_init);
 
module_exit(hello_exit);

ganquan@debian:~/Driver$ cat Makefile

#makefile for hello.ko
 
ifneq ($(KERNELRELEASE),)
 
obj-m := hello.o
 
else
 
KDIR ?= /lib/modules/$(shell uname -r)/build
 
PWD := $(shell pwd)
 
all:
 
$(MAKE) -C $(KDIR) M=$(PWD)
 
.PHONY:clean
 
clean:
 
rm -f *.mod.c *.mod.o *.ko *.o *.tmp_versions
 
endif

然后make就可以了,载入模块insmod hello.ko,卸载模块rmmod hello,查看内核空间输出dmesg | tail -N