ubuntu linux 内核开发环境,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. 意思是,如果你只是想为内核编译第三方的模块,那么,你不需下载此源码包。安装内核头文件包或许会更适合你。

2.如果你的Ubuntu是保持更新的,你的系统是安装有内核头文件包的,不信你到/usr/src目录下查看,是不是有Linux-headers-2.6.xx-xx-generic的文件夹呢,呵呵。我现在可以说,你可以在此开发你的驱动程序了。

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

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

4.环境本来就有了看来,所以我的文章,本来应该叫驱动开发最简单例子才合适……另,我要示例的是最简单,也是个最没用的驱动演示,名字叫tst,包含源码tst.c及makefile文件,www.Linuxidc.com编译出来的模块名叫tst.ko

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

[ 7559.570808] From tst module: Goodbye, Tony.    Note:呵呵,模块正常卸载

root@Ubuntu:/home/tony/mini2440/mtst#0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值