⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 node13.html

📁 Linux可卸载模块编程
💻 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">&#160;</A><A NAME="multi-file">&#160;</A><A NAME="183">&#160;</A>
<A NAME="184">&#160;</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">&#160;</A>
	<A NAME="193">&#160;</A>
	<A NAME="194">&#160;</A>
	<A NAME="195">&#160;</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>&lt;<TT>name of module</TT>&gt;<TT>.o 
	</TT>&lt;<TT>1st source file</TT>&gt;<TT>.o </TT>&lt;<TT>2nd source file</TT>&gt;<TT>.o</TT>.
	<A NAME="198">&#160;</A><A NAME="199">&#160;</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">&#160;</A><A NAME="206">&#160;</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 &lt;linux/kernel.h&gt;   /* We're doing kernel work */
#include &lt;linux/module.h&gt;   /* Specifically, a module */



/* Deal with CONFIG_MODVERSIONS */
#if CONFIG_MODVERSIONS==1
#define MODVERSIONS
#include &lt;linux/modversions.h&gt;
#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">&#160;</A><A NAME="214">&#160;</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 &lt;linux/kernel.h&gt;   /* We're doing kernel work */

#define __NO_VERSION__      /* This isn't "the" file 
                             * of the kernel module */
#include &lt;linux/module.h&gt;   /* Specifically, a module */

#include &lt;linux/version.h&gt;   /* Not included by 
                              * module.h because 
                              * of the __NO_VERSION__ */



/* Deal with CONFIG_MODVERSIONS */
#if CONFIG_MODVERSIONS==1
#define MODVERSIONS
#include &lt;linux/modversions.h&gt;
#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">&#160;</A><A NAME="222">&#160;</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 + -