📄 node12.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>Makefiles for Kernel Modules</TITLE>
<META NAME="description" CONTENT="Makefiles for 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="next" HREF="node13.html">
<LINK REL="previous" HREF="node11.html">
<LINK REL="up" HREF="node11.html">
<LINK REL="next" HREF="node13.html">
</HEAD>
<BODY >
<!--Navigation Panel-->
<A NAME="tex2html530"
HREF="node13.html">
<IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next"
SRC="next_motif.gif"></A>
<A NAME="tex2html526"
HREF="node11.html">
<IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up"
SRC="up_motif.gif"></A>
<A NAME="tex2html520"
HREF="node11.html">
<IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous"
SRC="previous_motif.gif"></A>
<A NAME="tex2html528"
HREF="node1.html">
<IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents"
SRC="contents_motif.gif"></A>
<A NAME="tex2html529"
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="tex2html531"
HREF="node13.html">Multiple File Kernel Modules</A>
<B> Up:</B> <A NAME="tex2html527"
HREF="node11.html">Hello, world</A>
<B> Previous:</B> <A NAME="tex2html521"
HREF="node11.html">Hello, world</A>
<BR>
<BR>
<!--End of Navigation Panel-->
<H1><A NAME="SECTION00310000000000000000"> </A><A NAME="makefile"> </A><A NAME="128"> </A>
<BR>
Makefiles for Kernel Modules
</H1>
<P>
A kernel module is not an independant executable, but an object file which
will be linked into the kernel in runtime. As a result, they should be
compiled with the <TT>-c</TT> flag. Also, all kernel modules have to be compiled
with certain symbols defined.
<A NAME="130"> </A>
<P>
<UL>
<P>
<LI><TT>__KERNEL__</TT> -- This tells the header files that this code will
be run in kernel mode, not as part of a user process.
<A NAME="133"> </A>
<P>
<LI><TT>MODULE</TT> -- This tells the header files to give the appropriate
definitions for a kernel module.
<A NAME="135"> </A>
<P>
<LI><TT>LINUX</TT> -- Technically speaking, this is not necessary. However,
if you ever want to write a serious kernel module which will compile
on more than one operating system, you'll be happy you did. This will
allow you to do conditional compilation on the parts which are OS
dependant.
<A NAME="137"> </A>
</UL>
<P>
There are other symbols which have to be included, or not, depending on
the flags the kernel was compiled with. If you're not sure how the kernel
was compiled, look it up in <TT>/usr/include/linux/config.h</TT>
<A NAME="140"> </A>
<A NAME="141"> </A>
<A NAME="142"> </A>
<P>
<UL>
<P>
<LI><TT>__SMP__</TT> -- Symmetrical MultiProcessing. This has to be
defined if the kernel was compiled to support symmetrical
multiprocessing (even if it's running just on one CPU). If
you use Symmetrical MultiProcessing, there are other things you need
to do (see chapter <A HREF="node26.html#smp">12</A>).
<A NAME="146"> </A>
<P>
<LI><TT>CONFIG_MODVERSIONS</TT> -- If CONFIG_MODVERSIONS was enabled, you
need to have it defined when compiling the kernel module and and to
include <TT>/usr/include/linux/modversions.h</TT>. This can also be
done by the code itself.
<A NAME="149"> </A>
<A NAME="150"> </A>
<P>
</UL>
<P>
ex
<FONT SIZE="+1"><B>Makefile</B></FONT>
<A NAME="156"> </A><A NAME="157"> </A>
<P>
<PRE>
# Makefile for a basic kernel module
CC=gcc
MODCFLAGS := -Wall -DMODULE -D__KERNEL__ -DLINUX
hello.o: hello.c /usr/include/linux/version.h
$(CC) $(MODCFLAGS) -c hello.c
echo insmod hello.o to turn it on
echo rmmod hello to turn if off
echo
echo X and kernel programming do not mix.
echo Do the insmod and rmmod from outside X.
</PRE>
<P>
So, now the only thing left is to <TT>su</TT> to root (you didn't compile this
as root, did you? Living on the edge<A NAME="tex2html21"
HREF="footnode.html#foot161"><SUP>1.1</SUP></A>...), and then
<TT>insmod hello</TT> and <TT>rmmod hello</TT> to your heart's content. While you
do it, notice your new kernel module in <TT>/proc/modules</TT>.
<A NAME="165"> </A>
<A NAME="166"> </A>
<A NAME="167"> </A>
<A NAME="168"> </A>
<P>
By the way, the reason why the Makefile recommends against doing <TT>insmod</TT>
from X
is because when the kernel has a message to print with <TT>printk</TT>, it
sends it to the console.
When you don't use X, it just goes to the virtual terminal you're using
(the one you chose with Alt-F<n>) and you see it. When you do use X, on the
other hand, there are two possibilities. Either you have a console open
with <TT>xterm -C</TT>, in which case the output will be sent there, or you don't,
in which case the output will go to virtual terminal 7 -- the one `covered'
by X.
<A NAME="172"> </A>
<A NAME="173"> </A>
<A NAME="174"> </A>
<A NAME="175"> </A>
<A NAME="176"> </A>
<A NAME="177"> </A>
<P>
If your kernel becomes unstable
you're likelier to get the debug messages without X. Outside of X,
<TT>printk</TT>
goes directly from the kernel to the console. In X, on the other hand,
<TT>printk</TT>'s go to a user mode process (<TT>xterm -C</TT>). When that process
receives CPU time, it is supposed to send it to the X server process.
Then, when the X server receives the CPU, it is supposed to display it --
but an unstable kernel usually means that the system is about to crash or
reboot, so you don't want to delay the error messages, which might explain
to you what went wrong, for longer than you have to.
<P>
<HR>
<!--Navigation Panel-->
<A NAME="tex2html530"
HREF="node13.html">
<IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next"
SRC="next_motif.gif"></A>
<A NAME="tex2html526"
HREF="node11.html">
<IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up"
SRC="up_motif.gif"></A>
<A NAME="tex2html520"
HREF="node11.html">
<IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous"
SRC="previous_motif.gif"></A>
<A NAME="tex2html528"
HREF="node1.html">
<IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents"
SRC="contents_motif.gif"></A>
<A NAME="tex2html529"
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="tex2html531"
HREF="node13.html">Multiple File Kernel Modules</A>
<B> Up:</B> <A NAME="tex2html527"
HREF="node11.html">Hello, world</A>
<B> Previous:</B> <A NAME="tex2html521"
HREF="node11.html">Hello, world</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 + -