查看“︁驱动第一发:hello world模块”︁的源代码
←
驱动第一发:hello world模块
跳转到导航
跳转到搜索
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于这些用户组的用户执行:*、
用户
您可以查看和复制此页面的源代码。
在编写代码之前,先要建立内核树,这里暂不予以讨论。 编译一个模块一般至少包括两个部分: *c源文件 <pre class="prettyprint"> //------------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 "Goodbye, cruel world\n"); } module_init(hello_init); module_exit(hello_exit); </pre> *Makefile <pre class="prettyprint"> //------------Makefile---------------------- obj-m := hello.o KERNELDIR := /lib/modules/$(shell uname -r)/build PWD := $(shell pwd) modules: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules #注意前面必须为tab modules_install: $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install #注意前面必须为tab </pre> 最后以root身份: $:make 然后加载模块: $:insmod ./hello.ko 卸载模块: $:rmmod hello 查看 /var/log/syslog验证模块加载信息: Feb 10 15:31:18 tgdn-1828 kernel: [ 190.982429] Hello world Feb 10 15:31:28 tgdn-1828 kernel: [ 200.771173] Goodbye, cruel world 或直接通过命令查看打印消息: $:dmesg
返回
驱动第一发:hello world模块
。
导航菜单
个人工具
创建账号
登录
命名空间
页面
讨论
大陆简体
查看
阅读
查看源代码
设置
查看历史
更多
搜索
导航
首页
Ubuntu
Android
C&CPP
Java
Python
AI
大杂烩
最近更改
工具
所有页面
文件列表
页面信息
特殊页面