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中的大部分驱动程序,是以模块的形式编写的。这些驱动程序源码可以修改到内核中,也可以把他们编译成模块形势,在需要的时候动态加载。0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值