Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1271407
  • 博文数量: 482
  • 博客积分: 13297
  • 博客等级: 上将
  • 技术积分: 2890
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-12 16:25
文章分类

全部博文(482)

文章存档

2012年(9)

2011年(407)

2010年(66)

分类: LINUX

2011-07-15 17:40:54

linux下加载一个简单的模块方法以及步骤:

运行环境:linux-2.6.12

编译环境:arm-linux-gcc(3.4.1)

运行平台:S3C2440

1.编写模块程序Module.c

 

#include

#include

#include

static int hello_init(void)

{

    printk("Hello, SmallBox! This is the first test module!\n");

    return 0;

}

static void hello_exit(void)

{

      printk("Small.Box Bye Bye!\n");

    return;

}

 

module_init(hello_init);

module_exit(hello_exit);

 

MODULE_LICENSE("GPL");

 

2.编写Makefile

obj-m += Module.o

KDIR:=/home/smallbox/hyh24x0_2.6.12/

PWD=$(shell pwd)

all:

       make -C $(KDIR) M=$(PWD) modules

clean:

       rm -rf *.o

注意:"make前面要空一个"Tab"

KDIR 为内核的路径,这个内核要与S3C2440运行的内核相同(编译器也要相同的,要不运行不了)。


/home/smallbox/hyh24x0_2.6.12/   是arm-linux下的内核


3.编译

linux下执行:make

/*注释:/usr/local/arm/3.4.1/bin/arm-linux-gcc 为交叉编译环境的路径*/

(make调用KDIR下的Makefile文件,文件指定了编译器为arm-linux-gcc)

生成Module.ko

 

4.运行

 

  Module.ko通过串口或者网口下载到S3C2440的板子上

  执行:chmod +x Module.ko修改模块的属性将其设为可执行文件

  执行:insmod Module.ko

Hello, SmallBox! This is the first test module!

执行:rmmod Module.ko

       Small.Box Bye Bye!


====

http://heyunhuan513.blog.163.com/blog/static/16020422008101151115321/

阅读(581) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~