<HTML ><HEAD ><TITLE >Modules Spanning Multiple Files</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="The Linux Kernel Module Programming Guide" HREF="book1.htm"><LINK REL="UP" TITLE="Hello World" HREF="c147.htm"><LINK REL="PREVIOUS" TITLE="Passing Command Line Arguments to a Module" HREF="x333.htm"><LINK REL="NEXT" TITLE="Building modules for a precompiled kernel" HREF="x419.htm"></HEAD ><BODY CLASS="SECT1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >The Linux Kernel Module Programming Guide</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="x333.htm" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Chapter 2. Hello World</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="x419.htm" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="SECT1" ><H1 CLASS="SECT1" ><A NAME="AEN365" ></A >Modules Spanning Multiple Files</H1 ><A NAME="AEN367" ></A ><A NAME="AEN370" ></A ><A NAME="AEN372" ></A ><A NAME="AEN374" ></A ><A NAME="AEN376" ></A ><A NAME="AEN378" ></A ><A NAME="AEN380" ></A ><P >Sometimes it makes sense to divide a kernel module between several source files. In this case, you need to:</P ><P ></P ><OL TYPE="1" ><LI ><P >In all the source files but one, add the line <B CLASS="COMMAND" >#define __NO_VERSION__</B >. This is important because <TT CLASS="FILENAME" >module.h</TT > normally includes the definition of <TT CLASS="VARNAME" >kernel_version</TT >, a global variable with the kernel version the module is compiled for. If you need <TT CLASS="FILENAME" >version.h</TT >, you need to include it yourself, because <TT CLASS="FILENAME" >module.h</TT > won't do it for you with <TT CLASS="VARNAME" >__NO_VERSION__</TT >.</P ></LI ><LI ><P >Compile all the source files as usual.</P ></LI ><LI ><P >Combine all the object files into a single one. Under x86, use <B CLASS="COMMAND" >ld -m elf_i386 -r -o <module name.o> <1st src file.o> <2nd src file.o></B >.</P ></LI ></OL ><P >The makefile will, once again, save us from having to get our hands dirty with compiling and linking the object files.</P ><P >Here's an example of such a kernel module.</P ><A NAME="AEN399" ></A ><DIV CLASS="EXAMPLE" ><A NAME="AEN402" ></A ><P ><B >Example 2-8. start.c</B ></P ><PRE CLASS="PROGRAMLISTING" >/* * start.c - Illustration of multi filed modules */ #include <linux/kernel.h> /* We're doing kernel work */ #include <linux/module.h> /* Specifically, a module */ int init_module(void) { printk("Hello, world - this is the kernel speaking\n"); return 0; }</PRE ></DIV ><P >The next file:</P ><A NAME="AEN407" ></A ><DIV CLASS="EXAMPLE" ><A NAME="AEN410" ></A ><P ><B >Example 2-9. stop.c</B ></P ><PRE CLASS="PROGRAMLISTING" >/* * stop.c - Illustration of multi filed modules */ #include <linux/kernel.h> /* We're doing kernel work */ #include <linux/module.h> /* Specifically, a module */ void cleanup_module() { printk("<1>Short is the life of a kernel module\n"); }</PRE ></DIV ><P >And finally, the makefile:</P ><DIV CLASS="EXAMPLE" ><A NAME="AEN415" ></A ><P ><B >Example 2-10. Makefile</B ></P ><PRE CLASS="PROGRAMLISTING" >obj-m += hello-1.o obj-m += hello-2.o obj-m += hello-3.o obj-m += hello-4.o obj-m += hello-5.o obj-m += startstop.o startstop-objs := start.o stop.o</PRE ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="x333.htm" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="book1.htm" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="x419.htm" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Passing Command Line Arguments to a Module</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="c147.htm" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Building modules for a precompiled kernel</TD ></TR ></TABLE ></DIV ></BODY ></HTML >