Chinaunix

标题: 装载和卸载模块-linux-2.6.20 for at91sam9263 [打印本页]

作者: senglen    时间: 2007-11-19 16:23
标题: 装载和卸载模块-linux-2.6.20 for at91sam9263
先简单编写一个hello world的程序,如下:
#ifndef __KERNEL__
#define __KERNEL__
#endif
#ifndef MODULE
#define MODULE
#endif

#include
#include
#include

MODULE_LICENSE("GPL");

static int hello_init(void)
{
  printk("Hello world\n");
  return 0;
}

static void hello_exit(void)
{
  printk("Goodbye world\n");
}

module_init(hello_init);
module_exit(hello_exit);

再写一个makefile文件:
#如果已定义KERNELRELEASE,则说明是从内核构造系统调用的,
#因此可利用其内语句。
ifneq ($(KERNELRELEASE),)
obj-m := hello.o
#否则,是直接从命令调用的,
#这时要调用内核构造系统。
else
#指定内核目录
        KERNELDIR ?=/home/at91sam9263ek/linux-2.6.20/
        PWD:=$(shell pwd)
default:
        $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
clean:
        rm -rf *.o *~core .depend .*.cmd *.mod.c .tmp_versions
endif


编译完后,得出如下:
[root@root linux_module]# ls
hello.c  hello.ko  hello.mod.c  hello.mod.o  hello.o  Makefile  Module.symvers
把hello.ko复制到开发板中,再:
/home # insmod hello.ko
Hello World


/home # rmmod hello   
Goodbye world




本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/26525/showart_426054.html




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2