linux驱动helloworld
生活随笔
收集整理的這篇文章主要介紹了
linux驱动helloworld
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
環(huán)境:centOS5;x86_64
[root@test include]# uname -r 2.6.18-308.8.2.el5首先構(gòu)建一個內(nèi)核樹:上http://www.kernel.org/pub/linux/kernel/?找對應(yīng)版本。下載 解壓到/usr/src/目錄
把usr/include/里的asm linux scsi重定向到新下載的內(nèi)核中
#rm -rf /usr/include/asm /usr/include/linux /usr/include/scsi #ln -s /usr/src/linux-2.6.18/include/asm-x86_64 /usr/include/asm #ln -s /usr/src/linux-2.6.18/include/linux /usr/include/linux #ln -s /usr/src/linux-2.6.18/include/scsi /usr/include/scsi開始編譯
#cd /usr/src/linux-2.6.18 #make mrproper #make defconfig #make #make modules #make modules_install #make install編譯完成。可以make help | less看make選項的意思
開始helloworld
#cd #mkdir -p /dv/hello #cd /dv/hello #vi hello.chello.c
#include <linux/init.h> #include <linux/module.h>MODULE_LICENSE("DUAL BSD/GPL");static int hello_init(void) {printk(KERN_ALERT "dv_hello world\n");return 0; }static void hello_exit(void) {printk(KERN_ALERT "dv_goodbye\n"); }module_init(hello_init); module_exit(hello_exit);Makefile?
obj-m := hello.o PWD := $(shell pwd) KVER := $(shell uname -r) KDIR := /lib/modules/$(KVER)/build all:$(MAKE) -C $(KDIR) M=$(PWD) modulesclean:rm -f *.o *.ko *.mod.c .hello*make一下生成一堆文件
[root@test hello]# ls hello.c hello.mod.c hello.o Module.markers hello.ko hello.mod.o Makefile Module.symvers?
加載模塊和卸載模塊
#insmod ./hello.ko #rmmod hello看看日志#cat /var/log/messages,可以看到hello.ko打印出的信息
Nov 8 17:47:35 test kernel: hello world Nov 8 17:47:38 test kernel: goodbye為了效果更明顯可以用tmux分出一個屏用watch -n 1 tail -n 3 /var/log/messages 或者tail -f /var/log/messages來監(jiān)視日志輸出
轉(zhuǎn)載于:https://www.cnblogs.com/Leo-Forest/archive/2012/11/14/2770741.html
總結(jié)
以上是生活随笔為你收集整理的linux驱动helloworld的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 批量替换 MySQL 指定字段中的字符串
- 下一篇: linux 其他常用命令