免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 966 | 回复: 0
打印 上一主题 下一主题

嵌入式Linux2.6 Kernel Module模板 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-06-21 11:35 |只看该作者 |倒序浏览
嵌入式Linux2.6 Kernel Module模板
/* hello.c */
#include        /* Needed by all modules */
#include          /* Needed for the module-macros */

static int __init hello_init(void)    // Module entry function specified by module_init()
{
        printk("Hello,world!\n");
        return 0;
}

static void __exit hello_exit(void)  //Module exit function specified by module_exit()
{
        printk("Goodbye,cruel world!\n");
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("Dual BSD/GPL");  //should always exist or you’ll get a warning
MODULE_AUTHOR("BENSON"); //optional
MODULE_DESCRIPTION("STUDY_MODULE"); //optional

/* Makefile */
# Makefile 2.6
obj-m += hello.o
KDIR:=/work/src/kernel/linux-2.6.18_pro500
#The source code directory of the kernel which runs on the development board
CC = arm-linux-gcc
#PWD=$(shell pwd)
all:
        make -C $(KDIR) M=$(PWD) modules
clean:
        make -C $(KDIR) M=$(PWD) clean

obj-m := hello.o表示编译后生成hello.o模块。
$(KDIR) 指定了内核源码的路径,“M=”表示这是个外部模块,M=$(PWD) 指定了该模块文件所在的路径。
注: makefile预定义了$(PWD)变量,此处可以不必重复定义。

执行#make编译成功后将生成的hello.ko拷贝到nfs文件系统。

加载到开发板上运行
#insmod hello.ko
Hello,world!
#lsmod 输出内核已加载模块信息,可以查看到刚刚加载成功的hello模块
……
Module                  Size  Used by
hello                   5632  0

卸载模块
#rmmod hello.ko
Goodbye,cruel world!
#lsmod 发现hello模块已经被卸载


参考资料:
The Linux Kernel Module Programming Guide 2.6  非常好的kernel module program 入门教程


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/60011/showart_1006547.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP