手机版
你好,游客 登录 注册
背景:
阅读新闻

Linux内核模块编译详解

[日期:2007-04-17] 来源:Linux公社  作者:Linux [字体:大 中 小]

同其他的程序设计教程一样 ,我们给出一个hello world 的例子:

/*hello.c a module programm*/ 

/* the program runing under kernel mod and it is a module*/ 

#include" linux/kernerl.h" 

#include"llinux/module.h" 

/* pross the CONFIG_MODVERSIONS*/ 

#if CONFIG_MODVERSIONS==1 

#define MODVERSIONS 

#include""linux/modversions.h" 

#end if 

  

/* the init function*/ 

int init_module() 

{ 

printk(" hello world !n'); 

printd(" I have runing in a kerner mod@!!n"); 

return 1; 

} 

/* the distory function*/ 

int cleanup_module() 

{ 

printk(" I will shut down myself in kernerl mod /n)"; 

retutn 0; 

}

这样一个例子就完成了。我们也写一个makefile 的例子,以适于我们在大程序重的应用。一下是makfile 文件的内容:

# a makefile for a module 

CC=gcc 

MODCFLAGS:= -Wall _DMODULE -D_KERNEL_ -Dlinux 

hello.o hello.c /usr/inculde?linux/version.h 

CC $(MODCFLAGS) 0c hello.c 

echo the module is complie completely

然后你运行make 命令 得到hello.o 这个模块运行:

$insmod hello.o 

hello world! 

I will shut down myself in kernerl mod 

$lsmod 

hello (unused) 

…

$remmod 

I will shut down myself in kernerl mod

这样你的模块就可以随意的插入和删除了。linux中的大部分驱动程序,是以模块的形式编写的。这些驱动程序源码可以修改到内核中,也可以把他们编译成模块形势,在需要的时候动态加载。

linux
【内容导航】
第1页:Linux内核模块编译详解 第2页:Linux内核模块编译详解
相关资讯       Linux 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款