小盒子的小盒

【原创】ARM-Linux模块编译与加载

0
阅读(2252)

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

运行环境:linux-2.6.12

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

运行平台:S3C2440

1.编写模块程序Module.c

 

#include <linux/init.h>

#include <linux/kernel.h>

#include <linux/module.h>

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 为交叉编译环境的路径*/

生成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!


转载说明:转载请表明作者以及出处,对于本博客的技术仅供参考。