📄 多文件内核模块.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0059)http://yangxingjun.myrice.com/chinesehow/kernel/node13.html -->
<!--Converted with LaTeX2HTML 98.1 release (February 19th, 1998)originally by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds* revised and updated by: Marcus Hennecke, Ross Moore, Herb Swan* with significant contributions from: Jens Lippmann, Marek Rouchal, Martin Wilck and others --><HTML><HEAD><TITLE>多文件内核模块</TITLE>
<META content="Multiple File Kernel Modules" name=description>
<META content=mpg name=keywords>
<META content=document name=resource-type>
<META content=global name=distribution>
<META http-equiv=Content-Type content="text/html; charset=gb2312"><LINK
href="C:\Documents and Settings\nonoka\My Documents\多文件内核模块.files\error(2).htm"
rel=STYLESHEET><LINK href="node12.html" rel=previous><LINK href="node11.html"
rel=up><LINK href="node14.html" rel=next>
<META content="MSHTML 6.00.2800.1106" name=GENERATOR></HEAD>
<BODY><!--Navigation Panel--><A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node14.html"
name=tex2html540><IMG height=24 alt=next src="多文件内核模块.files/next_motif.gif"
width=37 align=bottom border=0></A> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node11.html"
name=tex2html536><IMG height=24 alt=up src="多文件内核模块.files/error.htm" width=26
align=bottom border=0></A> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node12.html"
name=tex2html532><IMG height=24 alt=previous
src="C:\Documents and Settings\nonoka\My Documents\多文件内核模块.files\error(1).htm"
width=63 align=bottom border=0></A> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node1.html"
name=tex2html538><IMG height=24 alt=contents
src="多文件内核模块.files/contents_motif.gif" width=65 align=bottom border=0></A> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node34.html"
name=tex2html539><IMG height=24 alt=index src="多文件内核模块.files/index_motif.gif"
width=43 align=bottom border=0></A> <BR><B>Next:</B> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node14.html"
name=tex2html541>字符设备文件</A> <B>Up:</B> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node11.html"
name=tex2html537>Hello, world</A> <B>Previous:</B> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node12.html"
name=tex2html533>内核模块的Make文件</A> <BR><BR><!--End of Navigation Panel-->
<H1><A name=SECTION00320000000000000000> </A><A
name=multi-file> </A><A name=183> </A> <A name=184> </A>
<BR>多文件内核模块 </H1>
<P>有时候将内核模块分为几个源文件是有意义的,你需要做下面的事情:
<P>
<DL compact>
<DD>
<P></P>
<DT>1.
<DD>除了一个文件外在所有的源文件中加入一行 <TT>#define __NO_VERSION__</TT>.
这是很重要的,因为<TT>module.h</TT>通常包含<TT>kernel_version</TT>的定义,
它是一个和模块一起编译的内核版本的全局变量。如果你需要<TT>version.h</TT>, 你需要自己包含它,因为<TT>module.h</TT> 在有
<TT>__NO_VERSION__</TT>的定义的情况下不自动包含它。 <A name=192> </A> <A
name=193> </A> <A name=194> </A> <A name=195> </A>
<P></P>
<DT>2.
<DD>像通常那样编译所有的源文件。
<P></P>
<DT>3.
<DD>将所有的目标文件合并成一个。在x86架构下使用 <TT>ld -m elf_i386 -r -o </TT><<TT>
模块名</TT>><TT>.o </TT><<TT>第一个源文件名</TT>><TT>.o
</TT><<TT>第二个源文件名</TT>><TT>.o</TT>. <A name=198> </A><A
name=199> </A>
<P></P></DD></DL>
<P>这里有一个这样的内核模块的范例。
<P>范例 <FONT size=+1><B>start.c</B></FONT> <A name=205> </A><A
name=206> </A>
<P><PRE>
/* start.c
* Copyright (C) 1999 by Ori Pomerantz
*
* "Hello, world" - 内核模块版本.
* 这个文件只包含启动程序
*/
/* 必要的头文件 */
/* 内核模块标准头文件 */
#include <linux/kernel.h> /* 我们正在做内核的工作 */
#include <linux/module.h> /* 明确的指定是内核模块 */
/* 处理 CONFIG_MODVERSIONS */
#if CONFIG_MODVERSIONS==1
#define MODVERSIONS
#include <linux/modversions.h>
#endif
/* 初始化模块 */
int init_module()
{
printk("Hello, world - this is the kernel speaking\n");
/* If we return a non zero value, it means that
* init_module failed and the kernel module
* can't be loaded */
return 0;
}
</PRE>
<P>范例 <FONT size=+1><B>stop.c</B></FONT> <A name=213> </A><A
name=214> </A>
<P><PRE>
/* stop.c
* Copyright (C) 1999 by Ori Pomerantz
*
* "Hello, world" - 内核模块版本
* 这个文件只包含终止程序
*/
/* 必要的头文件 */
/* 内核模块的标准头文件 */
#include <linux/kernel.h> /* 我们正在做内核的工作 */
#define __NO_VERSION__ /* 这不是内核模块文件 */
#include <linux/module.h> /* 明确的指定是内核模块 */
#include <linux/version.h> /* 因为有 __NO_VERSION__ 而不能被自动包含*/
/* 处理 CONFIG_MODVERSIONS */
#if CONFIG_MODVERSIONS==1
#define MODVERSIONS
#include <linux/modversions.h>
#endif
/* Cleanup - 撤消init_module 所做的任何事情 */
void cleanup_module()
{
printk("Short is the life of a kernel module\n");
}
</PRE>
<P>范例 <FONT size=+1><B>Makefile</B></FONT> <A name=221> </A><A
name=222> </A>
<P><PRE>
# 多文件内核模块的Make文件
CC=gcc
MODCFLAGS := -Wall -DMODULE -D__KERNEL__ -DLINUX
hello.o: start.o stop.o
ld -m elf_i386 -r -o hello.o start.o stop.o
start.o: start.c /usr/include/linux/version.h
$(CC) $(MODCFLAGS) -c start.c
stop.o: stop.c /usr/include/linux/version.h
$(CC) $(MODCFLAGS) -c stop.c
</PRE>
<P>
<HR>
<!--Navigation Panel--><A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node14.html"
name=tex2html540><IMG height=24 alt=next src="多文件内核模块.files/next_motif.gif"
width=37 align=bottom border=0></A> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node11.html"
name=tex2html536><IMG height=24 alt=up src="多文件内核模块.files/error.htm" width=26
align=bottom border=0></A> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node12.html"
name=tex2html532><IMG height=24 alt=previous
src="C:\Documents and Settings\nonoka\My Documents\多文件内核模块.files\error(1).htm"
width=63 align=bottom border=0></A> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node1.html"
name=tex2html538><IMG height=24 alt=contents
src="多文件内核模块.files/contents_motif.gif" width=65 align=bottom border=0></A> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node34.html"
name=tex2html539><IMG height=24 alt=index src="多文件内核模块.files/index_motif.gif"
width=43 align=bottom border=0></A> <BR><B>Next:</B> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node14.html"
name=tex2html541>字符设备文件</A> <B>Up:</B> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node11.html"
name=tex2html537>Hello, world</A> <B>Previous:</B> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node12.html"
name=tex2html533>内核模块的Make文件</A> <!--End of Navigation Panel-->
<ADDRESS><I></I><BR><I>1999-05-19</I> </ADDRESS></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -