Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > d10250e1485b7c1121583bcab932c663 > files > 38

lkmpg-1.1.0-19.mga4.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML
><HEAD
><TITLE
>Hello World (part 2)</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
REL="HOME"
TITLE="The Linux Kernel Module Programming Guide"
HREF="index.html"><LINK
REL="UP"
TITLE="Hello World"
HREF="c147.html"><LINK
REL="PREVIOUS"
TITLE="Compiling Kernel Modules"
HREF="x208.html"><LINK
REL="NEXT"
TITLE="Hello World (part 3): The __init and __exit Macros"
HREF="x281.html"></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="x208.html"
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="x281.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="HELLO2"
></A
>2.3. Hello World (part 2)</H1
><P
>As of Linux 2.4, you can rename the init and cleanup functions of your modules; they no longer have to be called
	<TT
CLASS="FUNCTION"
>init_module()</TT
> and <TT
CLASS="FUNCTION"
>cleanup_module()</TT
> respectively.  This is done with the
	<TT
CLASS="FUNCTION"
>module_init()</TT
> and <TT
CLASS="FUNCTION"
>module_exit()</TT
> macros.  These macros are defined in <TT
CLASS="FILENAME"
>linux/init.h</TT
>.  The only caveat is that your init and cleanup functions must be defined before calling
	the macros, otherwise you'll get compilation errors.  Here's an example of this technique:</P
><DIV
CLASS="EXAMPLE"
><A
NAME="AEN272"
></A
><P
><B
>Example 2-3. hello-2.c</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="PROGRAMLISTING"
>/*  hello-2.c - Demonstrating the module_init() and module_exit() macros.  This is the 
 *     preferred over using init_module() and cleanup_module().
 */
#include &#60;linux/module.h&#62;   // Needed by all modules
#include &#60;linux/kernel.h&#62;   // Needed for KERN_ALERT
#include &#60;linux/init.h&#62;     // Needed for the macros


static int hello_2_init(void)
{
   printk(KERN_ALERT "Hello, world 2\n");
   return 0;
}


static void hello_2_exit(void)
{
   printk(KERN_ALERT "Goodbye, world 2\n");
}


module_init(hello_2_init);
module_exit(hello_2_exit);</PRE
></FONT
></TD
></TR
></TABLE
></DIV
><P
>So now we have two real kernel modules under our belt.  With productivity as high as ours, we should have a high powered
	Makefile.  Here's a more advanced Makefile which will compile both our modules at the same time.  It's optimized for brevity
	and scalability.  If you don't understand it, I urge you to read the makefile info pages or the GNU Make Manual.</P
><DIV
CLASS="EXAMPLE"
><A
NAME="AEN276"
></A
><P
><B
>Example 2-4. Makefile for both our modules</B
></P
><TABLE
BORDER="1"
BGCOLOR="#E0E0E0"
WIDTH="100%"
><TR
><TD
><FONT
COLOR="#000000"
><PRE
CLASS="SCREEN"
>WARN    := -W -Wall -Wstrict-prototypes -Wmissing-prototypes
INCLUDE := -isystem /lib/modules/`uname -r`/build/include
CFLAGS  := -O2 -DMODULE -D__KERNEL__ ${WARN} ${INCLUDE}
CC      := gcc-3.0
OBJS    := ${patsubst %.c, %.o, ${wildcard *.c}}

all: ${OBJS}

.PHONY: clean

clean:
    rm -rf *.o</PRE
></FONT
></TD
></TR
></TABLE
></DIV
><P
>As an exercise to the reader, if we had another module in the same directory, say <TT
CLASS="FILENAME"
>hello-3.c</TT
>, how
	would you modify this Makefile to automatically compile that module?</P
></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="x208.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="x281.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Compiling Kernel Modules</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="c147.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Hello World (part 3): The <TT
CLASS="LITERAL"
>__init</TT
> and <TT
CLASS="LITERAL"
>__exit</TT
> Macros</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>