<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="book1.htm"><LINK REL="UP" TITLE="Hello World" HREF="c147.htm"><LINK REL="PREVIOUS" TITLE="Compiling Kernel Modules" HREF="x208.htm"><LINK REL="NEXT" TITLE="Hello World (part 3): The __init and __exit Macros" HREF="x281.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="x208.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="x281.htm" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="SECT1" ><H1 CLASS="SECT1" ><A NAME="HELLO2" ></A >Hello World (part 2)</H1 ><A NAME="AEN259" ></A ><A NAME="AEN261" ></A ><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 ><A NAME="AEN269" ></A ><DIV CLASS="EXAMPLE" ><A NAME="AEN272" ></A ><P ><B >Example 2-3. hello-2.c</B ></P ><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 <linux/module.h> // Needed by all modules #include <linux/kernel.h> // Needed for KERN_ALERT #include <linux/init.h> // 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 ></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 ><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 ></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.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="x281.htm" 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.htm" 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 >