Debian 6 驱动开发环境搭建
Debian 6 驱动开发环境搭建
1.安装相关工具
apt-getinstall-ygccg++gdbmakebuild-essential
2.查看系统版本,并安装内核头文件
[email protected]:~/#uname-r
2.6.32-5-686
apt-cachesearchlinux-headers-2.6.32-5-686
apt-getinstall-ylinux-headers-2.6.32-5-686
3.查看内核头文件位置
看下会安装到哪:
apt-cacheshowlinux-headers-2.6.32-5-686
有这么句:
Thesefilesaregoingtobeinstalledinto
/usr/src/linux-headers-2.6.32-5-686,andcanbeusedforbuildingmodules
就是这里啦.
/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");
staticinthello_init(void)
{
printk(KERN_ALERT"Hello,world\n");
return0;
}
staticvoidhello_exit(void)
{
printk(KERN_ALERT"Goodbye,cruelworld\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:=$(shellpwd)
.PHONY:testcleanall
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
rmmodhello
dmesg-c
5.好了,现在测试开始,成功的话,就可以看到下面的文字了.
如果有问题的话,向google大神请教吧.另外要看
[email protected]:~/cpp#make
make-C/usr/src/linux-headers-2.6.32-5-686M=/root/cppmodules
make[1]:Enteringdirectory`/usr/src/linux-headers-2.6.32-5-686'
CC[M]/root/cpp/hello.o
Buildingmodules,stage2.
MODPOST1modules
CC/root/cpp/hello.mod.o
LD[M]/root/cpp/hello.ko
make[1]:Leavingdirectory`/usr/src/linux-headers-2.6.32-5-686'
[email protected]:~/cpp#maketest
insmod./hello.ko
rmmodhello
dmesg-c
[673.500413]Hello,world
[673.504907]Goodbye,cruelworld
[email protected]:~/cpp#
6.最后.安装开发帮助文档
apt-getinstall-ymanpages-kernel-devlinux-manual
man9printk
man9module_init
参考资料:
http://bbs.chinaunix.net/thread-3570849-1-1.html