📄 node13.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<!--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>Multiple File Kernel Modules</TITLE>
<META NAME="description" CONTENT="Multiple File Kernel Modules">
<META NAME="keywords" CONTENT="mpg">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<LINK REL="STYLESHEET" HREF="mpg.css">
<LINK REL="previous" HREF="node12.html">
<LINK REL="up" HREF="node11.html">
<LINK REL="next" HREF="node14.html">
</HEAD>
<BODY >
<!--Navigation Panel-->
<A NAME="tex2html540"
HREF="node14.html">
<IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next"
SRC="next_motif.gif"></A>
<A NAME="tex2html536"
HREF="node11.html">
<IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up"
SRC="up_motif.gif"></A>
<A NAME="tex2html532"
HREF="node12.html">
<IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous"
SRC="previous_motif.gif"></A>
<A NAME="tex2html538"
HREF="node1.html">
<IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents"
SRC="contents_motif.gif"></A>
<A NAME="tex2html539"
HREF="node34.html">
<IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index"
SRC="index_motif.gif"></A>
<BR>
<B> Next:</B> <A NAME="tex2html541"
HREF="node14.html">Character Device Files</A>
<B> Up:</B> <A NAME="tex2html537"
HREF="node11.html">Hello, world</A>
<B> Previous:</B> <A NAME="tex2html533"
HREF="node12.html">Makefiles for Kernel Modules</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>
Multiple File Kernel Modules
</H1>
<P>
Sometimes it makes sense to divide a kernel module between several
source files. In this case, you need to do the following:
<P>
<DL COMPACT>
<DD><P>
<DT>1.
<DD>In all the source files but one, add the line <TT>#define
__NO_VERSION__</TT>. This is important because <TT>module.h</TT>
normally includes the definition of <TT>kernel_version</TT>, a global
variable with the kernel version the module is compiled for. If
you need <TT>version.h</TT>, you need to include it yourself, because
<TT>module.h</TT> won't do it for you with <TT>__NO_VERSION__</TT>.
<A NAME="192"> </A>
<A NAME="193"> </A>
<A NAME="194"> </A>
<A NAME="195"> </A>
<P>
<DT>2.
<DD>Compile all the source files as usual.
<P>
<DT>3.
<DD>Combine all the object files into a single one. Under x86, do it with
<TT>ld -m elf_i386 -r -o </TT><<TT>name of module</TT>><TT>.o
</TT><<TT>1st source file</TT>><TT>.o </TT><<TT>2nd source file</TT>><TT>.o</TT>.
<A NAME="198"> </A><A NAME="199"> </A>
<P>
</DL>
<P>
Here's an example of such a kernel module.
<P>
ex
<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" - the kernel module version.
* This file includes just the start routine
*/
/* The necessary header files */
/* Standard in kernel modules */
#include <linux/kernel.h> /* We're doing kernel work */
#include <linux/module.h> /* Specifically, a module */
/* Deal with CONFIG_MODVERSIONS */
#if CONFIG_MODVERSIONS==1
#define MODVERSIONS
#include <linux/modversions.h>
#endif
/* Initialize the module */
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>
ex
<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" - the kernel module version. This
* file includes just the stop routine.
*/
/* The necessary header files */
/* Standard in kernel modules */
#include <linux/kernel.h> /* We're doing kernel work */
#define __NO_VERSION__ /* This isn't "the" file
* of the kernel module */
#include <linux/module.h> /* Specifically, a module */
#include <linux/version.h> /* Not included by
* module.h because
* of the __NO_VERSION__ */
/* Deal with CONFIG_MODVERSIONS */
#if CONFIG_MODVERSIONS==1
#define MODVERSIONS
#include <linux/modversions.h>
#endif
/* Cleanup - undid whatever init_module did */
void cleanup_module()
{
printk("Short is the life of a kernel module\n");
}
</PRE>
<P>
ex
<FONT SIZE="+1"><B>Makefile</B></FONT>
<A NAME="221"> </A><A NAME="222"> </A>
<P>
<PRE>
# Makefile for a multifile kernel module
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 NAME="tex2html540"
HREF="node14.html">
<IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next"
SRC="next_motif.gif"></A>
<A NAME="tex2html536"
HREF="node11.html">
<IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up"
SRC="up_motif.gif"></A>
<A NAME="tex2html532"
HREF="node12.html">
<IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous"
SRC="previous_motif.gif"></A>
<A NAME="tex2html538"
HREF="node1.html">
<IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents"
SRC="contents_motif.gif"></A>
<A NAME="tex2html539"
HREF="node34.html">
<IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index"
SRC="index_motif.gif"></A>
<BR>
<B> Next:</B> <A NAME="tex2html541"
HREF="node14.html">Character Device Files</A>
<B> Up:</B> <A NAME="tex2html537"
HREF="node11.html">Hello, world</A>
<B> Previous:</B> <A NAME="tex2html533"
HREF="node12.html">Makefiles for Kernel Modules</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 + -