Debian 6 驅動開發環境搭建
1.安裝相關工具
apt-get install -y gcc g++ gdb make build-essential
2.查看系統版本,并安裝內核頭文件
root@192.168.20.128:~/ # uname -r
2.6.32-5-686
apt-cache search linux-headers-2.6.32-5-686
apt-get install -y linux-headers-2.6.32-5-686
3.查看內核頭文件位置
看下會安裝到哪:
apt-cache show linux-headers-2.6.32-5-686
有這么句:
These files are going to be installed into
/usr/src/linux-headers-2.6.32-5-686, and can be used for building modules
就是這里啦.
/usr/src/linux-headers-2.6.32-5-686
4.好了,接下來寫測試例子:hello,抄了http://bbs.chinaunix.net/thread-3570849-1-1.html
上的代碼:
文件1:hello.c
#include <linux/init.h>
#include <linux/module.h>
MODULE_LICENSE("GPL");
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:
obj-m +=hello.o
KERNELDIR := /usr/src/linux-headers-2.6.32-5-686
PWD :=$(shell pwd)
.PHONY: test clean all
all:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versionsm *.order *.symvers
test:
insmod ./hello.ko
rmmod hello
dmesg -c
5.好了,現在測試開始,成功的話,就可以看到下面的文字了.
如果有問題的話,向google大神請教吧.另外要看
root@192.168.20.128:~/cpp # make
make -C /usr/src/linux-headers-2.6.32-5-686 M=/root/cpp modules
make[1]: Entering directory `/usr/src/linux-headers-2.6.32-5-686'
CC [M] /root/cpp/hello.o
Building modules, stage 2.
MODPOST 1 modules
CC /root/cpp/hello.mod.o
LD [M] /root/cpp/hello.ko
make[1]: Leaving directory `/usr/src/linux-headers-2.6.32-5-686'
root@192.168.20.128:~/cpp # make test
insmod ./hello.ko
rmmod hello
dmesg -c
[ 673.500413] Hello, world
[ 673.504907] Goodbye, cruel world
root@192.168.20.128:~/cpp #
6.最后.安裝開發幫助文檔
apt-get install -y manpages-kernel-dev linux-manual
man 9 printk
man 9 module_init
參考資料:
http://bbs.chinaunix.net/thread-3570849-1-1.html