
[历史归档]本文原发布于 cstriker1407.info 个人博客内容为历史存档仅供参考。发布时间2014-02-28 标题驱动学习笔记HelloWorld分类编程 / 操作系统 / linux / C C / kernel 标签ubuntu·kernel驱动学习笔记HelloWorld最近突然想重新学习驱动开发在ubuntu上装好SSH之后环境基本上就OK了那么就先写一个最基础的helloworld。为了方便先切换到rootcstriker1407cstriker1407-ubuntu:~$sudopasswdpasswordforcstriker1407: 输入新的 UNIX 密码 重新输入新的 UNIX 密码 passwd已成功更新密码 cstriker1407cstriker1407-ubuntu:~$su- 密码 rootcstriker1407-ubuntu:~# mkdir helloworldrootcstriker1407-ubuntu:~# cd helloworld/然后新建两个文件【 helloworld.c 】#includelinux/kernel.h#includelinux/module.hMODULE_LICENSE(Dual BSD/GPL);staticint__inithello_init(void){printk(KERN_ALERT Hello world );return0;}staticvoid__exithello_exit(void){printk(KERN_ALERT Goodbye world );}module_init(hello_init);module_exit(hello_exit);【 Makefile 】obj-m : helloworld.o KERNEL_DIR : /lib/modules/$(shell uname -r)/build PWD : $(shell pwd) all: make -C $(KERNEL_DIR) SUBDIRS$(PWD) modules clean: rm *.o *.ko *.mod.c .PHONY:clean然后就可以编译了命令记录如下rootcstriker1407-ubuntu:~/helloworld# lshelloworld.c Makefile rootcstriker1407-ubuntu:~/helloworld# makemake-C/lib/modules/3.11.0-15-generic/buildSUBDIRS/root/helloworld modules make[1]: 正在进入目录/usr/src/linux-headers-3.11.0-15-generic CC[M]/root/helloworld/helloworld.o Building modules, stage2. MODPOST1modules CC /root/helloworld/helloworld.mod.o LD[M]/root/helloworld/helloworld.ko make[1]:正在离开目录/usr/src/linux-headers-3.11.0-15-generic rootcstriker1407-ubuntu:~/helloworld# lshelloworld.c helloworld.ko helloworld.mod.c helloworld.mod.o helloworld.o Makefile modules.order Module.symvers载入模块和卸载模块rootcstriker1407-ubuntu:~/helloworld# insmod helloworld.korootcstriker1407-ubuntu:~/helloworld# rmmod helloworld.ko此时应该会有日志打印到终端如下图如果不好关注终端也可以手动查看日志显示可用下面的两个命令dmesg|tailtail-f/var/log/kern.log