设为首页 加入收藏

TOP

Ubuntu下轻松建立你的内核驱动开发环境 tonychen123(一)
2014-11-24 03:06:13 来源: 作者: 【 】 浏览:3
Tags:Ubuntu 轻松 建立 内核 驱动 开发 环境 tonychen123

Preface:
1.笔者刚学Linux驱动开发,看到网上很多说要现建立内核树。在笔者到官网下载源码时,源码下面有如下说明: If you are simply trying to build third-party modules for your kernel, you do not want this package. Install the appropriate Linux-headers package instead. 意思是,如果你只是想为内核编译第三方的模块,那么,你不需下载此源码包。安装内核头文件包或许会更适合你。


3.笔者也是初学,有见笑的,大家多指正


emial: tonychen123@qq.com; 转载注明出处:http://sites.google.com/site/tonychen123site,作者:tony


Content:


1)建立你的实验目录并创建tst.c源文件:


代码:
tony@Ubuntu:~/mini2440$ mkdir mtst; cd mtst
tony@Ubuntu:~/mini2440/mtst$ vim tst.c


======================================



#include
#include
#include


static int __init tst_start(viod)
{
printk(KERN_INFO "Loading my so simple module...\n");
printk(KERN_INFO "I love Ubuntu\n");
return 0;
}


static void __exit tst_end(void)
{
printk(KERN_INFO "From tst module: Goodbye, Tony.\n");
}


module_init(tst_start);
module_exit(tst_end);




2)创建Makefile文件:



代码:
tony@Ubuntu:~/mini2440/mtst$ vim Makefile
=================================================================
obj-m = tst.o


K_DIR = $(shell uname -r)
PWD = $(shell pwd)


all:
make -C /lib/modules/$(K_DIR)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(K_DIR)/build M=$(PWD) clean


# end #



3)编译



代码:
tony@Ubuntu:~/mini2440/mtst$ sudo make Note:好像要用sudo才行!
make -C /lib/modules/2.6.28-12-generic/build M=/home/tony/mini2440/mtst modules
make[1]: Entering directory `/usr/src/Linux-headers-2.6.28-12-generic'
CC [M] /home/tony/mini2440/mtst/tst.o
/home/tony/mini2440/mtst/tst.c:19: warning: function declaration isn’t a prototype
Building modules, stage 2.
MODPOST 1 modules Note:生成一个module!
CC /home/tony/mini2440/mtst/tst.mod.o
LD [M] /home/tony/mini2440/mtst/tst.ko
make[1]: Leaving directory `/usr/src/Linux-headers-2.6.28-12-generic'
tony@Ubuntu:~/mini2440/mtst$



4)测试


代码:
tony@Ubuntu:~/mini2440/mtst$ su Note:加载模块要有root特权
Password:
root@Ubuntu:/home/tony/mini2440/mtst# insmod tst.ko  Note:加载模块要有root特权
root@Ubuntu:/home/tony/mini2440/mtst# dmesg | tail   
[ 25.464531] r8169: eth0: link up
[ 25.464534] r8169: eth0: link up
[ 54.931357] UDF-fs: No VRS found
[ 54.981587] ISO 9660 Extensions: Microsoft Joliet Level 3
[ 55.013712] ISOFS: changing to secondary root
[ 2932.746291] module init
[ 3138.097884] Cleanup module
[ 7444.574136] tst: module license 'unspecified' taints kernel.
[ 7444.575117] Loading my so simple module... Note:注意这两行,就是我们的模块加载信息
[ 7444.575121] I love Ubuntu
root@Ubuntu:/home/tony/mini2440/mtst# lsmod | grep tst
tst 9344 0
root@Ubuntu:/home/tony/mini2440/mtst# rmmod tst.ko
root@Ubuntu:/home/tony/mini2440/mtst# lsmod | grep tst
root@Ubuntu:/home/tony/mini2440/mtst# dmesg | tail
[ 25.464534] r8169: eth0: link up
[ 54.931357] UDF-fs: No VRS found
[ 54.981587] ISO 9660 Extensions: Microsoft Joliet Level 3
[ 55.013712] ISOFS: changing to secondary root
[ 2932.746291] module init
[ 3138.097884] Cleanup module
[ 7444.574136] tst: module license 'unspecified' taints kernel.
[ 7444.575117] Loading my so simple module...
[ 7444.575121] I love Ubuntu
[ 755

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Ubuntu 10.04 下Rails环境构建 下一篇Java实例-奇数的和

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Announcing October (2025-12-24 15:18:16)
·MySQL有什么推荐的学 (2025-12-24 15:18:13)
·到底应该用MySQL还是 (2025-12-24 15:18:11)
·进入Linux世界大门的 (2025-12-24 14:51:47)
·Download Linux | Li (2025-12-24 14:51:44)