在桌面红旗Linux6上编写并运行第一个驱动设备程序
在桌面红旗Linux6上编写并运行第一个驱动设备程序
在桌面红旗Linux6上编写并运行第一个驱动设备程序 ,使用根用户,创建一个文件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()
- {
- printk(KERN_ALERT "Bye bye, Hello, World\n");
- }
- module_init(hello_init);
- module_exit(hello_exit);
创建Makefile文件,内容是:
- # Makefile
- #
- obj-m:=hello.o
- KDIR:=/usr/src/kernels/2.6.27.10-1-i686
- PWD:=$(shell pwd)
- default:
- $(MAKE) -C $(KDIR) M=$(PWD) modules
- ~
(注:上述的“usr/src/kernels/2.6.27.10-1-i686”是桌面红旗6的源码目录)
然后:make
产生几个文件:hello.mod.c hello.o modules.order hello.ko hello.mod.o Module.symvers
运行驱设备程序:
insmod hello.ko
卸载程序:
rmmod hello
在x86的机器上,若syslog运行了,则"Hello, World\n"打印至“/var/log/messages”,可使用:
cat /var/log/messages
或
dmesg
查看!
评论暂时关闭