📄 内核模块的make文件.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0059)http://yangxingjun.myrice.com/chinesehow/kernel/node12.html -->
<!--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>内核模块的Make文件</TITLE>
<META content="Makefiles for Kernel Modules" name=description>
<META content=mpg name=keywords>
<META content=document name=resource-type>
<META content=global name=distribution>
<META http-equiv=Content-Type content="text/html; charset=gb2312"><LINK
href="C:\Documents and Settings\nonoka\My Documents\内核模块的Make文件.files\error(2).htm"
rel=STYLESHEET><LINK href="node13.html" rel=next><LINK href="node11.html"
rel=previous><LINK href="node11.html" rel=up><LINK href="node13.html" rel=next>
<META content="MSHTML 6.00.2800.1106" name=GENERATOR></HEAD>
<BODY><!--Navigation Panel--><A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node13.html"
name=tex2html530><IMG height=24 alt=next src="内核模块的Make文件.files/next_motif.gif"
width=37 align=bottom border=0></A> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node11.html"
name=tex2html526><IMG height=24 alt=up src="内核模块的Make文件.files/error.htm"
width=26 align=bottom border=0></A> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node11.html"
name=tex2html520><IMG height=24 alt=previous
src="C:\Documents and Settings\nonoka\My Documents\内核模块的Make文件.files\error(1).htm"
width=63 align=bottom border=0></A> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node1.html"
name=tex2html528><IMG height=24 alt=contents
src="内核模块的Make文件.files/contents_motif.gif" width=65 align=bottom border=0></A>
<A href="http://yangxingjun.myrice.com/chinesehow/kernel/node34.html"
name=tex2html529><IMG height=24 alt=index
src="内核模块的Make文件.files/index_motif.gif" width=43 align=bottom border=0></A>
<BR><B>Next:</B> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node13.html"
name=tex2html531>内核模块多文件</A> <B>Up:</B> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node11.html"
name=tex2html527>Hello, world</A> <B>Previous:</B> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node11.html"
name=tex2html521>Hello, world</A> <BR><BR><!--End of Navigation Panel-->
<H1><A name=SECTION00310000000000000000> </A><A name=makefile> </A><A
name=128> </A> <BR>内核模块的Make文件 </H1>
<P>一个内核模块单独是不可执行的,但目标文件在运行时被连接进内核。因此,在编译时要使用 <TT>-c</TT> 标记. 而且,
所有的内核模块必须结合特定的定义过的符号进行编译。 <A name=130> </A>
<P>
<UL>
<P>
<LI><TT>__KERNEL__</TT> -- 这个符号告诉头文件这些代码将在内核模式运行,不要当作用户进程的一部分。 <A
name=133> </A>
<P></P>
<LI><TT>MODULE</TT> -- 这个符号告诉头文件为内核模块给出适当的定义。 <A name=135> </A>
<P></P>
<LI><TT>LINUX</TT> -- 从技术上说这不是必要的。然而,如果你曾经想过写一系列要在不止一个的操作系统
上编译的内核模块,那么你将为你在这所做的高兴。这将允许你条件编译平台独立性的部分。 <A name=137> </A> </LI></UL>
<P>还有其他一些需要或不需要包括的符号,这取决于内核用什么标记编译。如果你不能确定内核是如何编译的,请查看
<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(对称多处理). 如果内核被编译为支持对称多处理
(即使它仅仅运行于单CPU上),这个符号必须包含.如果你使用对称多处理,你还有很多其他的事情需要 做(参看<A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node26.html#smp">第12章</A>).
<A name=146> </A>
<P></P>
<LI><TT>CONFIG_MODVERSIONS</TT> -- 如果CONFIG_MODVERSIONS 被激活, 你需要使它在编译内核模块时已定义
并且包含<TT>/usr/include/linux/modversions.h</TT>. 这也可以比、被代码自己完成。 <A
name=149> </A> <A name=150> </A>
<P></P></LI></UL>
<P>范例 <FONT size=+1><B>Makefile</B></FONT> <A name=156> </A><A
name=157> </A>
<P><PRE>
# 为基本的内核模块写的Make文件
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>因此,现在唯一剩下的事情就是<TT>su</TT> 为root用户 (你不能作为root用户编译,对吗? 生活在边缘<A
href="http://yangxingjun.myrice.com/chinesehow/kernel/footnode.html#foot161"
name=tex2html21><SUP>1.1</SUP></A>...), 然后<TT>insmod hello</TT> 和 <TT>rmmod
hello</TT> 到你的系统核心. 当你完成这个,注意 <TT>/proc/modules</TT>中的新内核模块。 <A
name=165> </A> <A name=166> </A> <A name=167> </A> <A
name=168> </A>
<P>顺便说一下,之所以推荐不要在 X下做<TT>insmod</TT>是因为当内核用<TT>printk</TT>打印消息时它将消息发送到控制台。当你不使用
X, 它就发往你正在使用的虚拟终端(你用Alt-F<n>选定的那个) 而使你可以看见。另一方面,当你使用 X,
有两种可能。要么你有一个用<TT>xterm
-C</TT>打开的终端,在这种情况下输出将被发送到那儿;或者你没有打开终端,在这种情况下输出被发往被X“隐蔽”的虚拟终端7。 <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>如果你的内核变得不稳定,不用X得到调试信息更有可能。在X之外<TT>printk</TT>
直接从内核打印到控制台。另一方面,在X下,<TT>printk</TT>变成用户模式进程 (<TT>xterm -C</TT>).
当该进程正占用CPU,它被假设发往X服务进程,然后,当X服务进程占用 CPU, 它被假设去显示该信息
--但是一个不稳定的内核通常意味着崩溃或重新启动然而你不想延迟得到错误信息,这也许可以向你解释什么地方出错了。
<P>
<HR>
<!--Navigation Panel--><A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node13.html"
name=tex2html530><IMG height=24 alt=next src="内核模块的Make文件.files/next_motif.gif"
width=37 align=bottom border=0></A> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node11.html"
name=tex2html526><IMG height=24 alt=up src="内核模块的Make文件.files/error.htm"
width=26 align=bottom border=0></A> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node11.html"
name=tex2html520><IMG height=24 alt=previous
src="C:\Documents and Settings\nonoka\My Documents\内核模块的Make文件.files\error(1).htm"
width=63 align=bottom border=0></A> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node1.html"
name=tex2html528><IMG height=24 alt=contents
src="内核模块的Make文件.files/contents_motif.gif" width=65 align=bottom border=0></A>
<A href="http://yangxingjun.myrice.com/chinesehow/kernel/node34.html"
name=tex2html529><IMG height=24 alt=index
src="内核模块的Make文件.files/index_motif.gif" width=43 align=bottom border=0></A>
<BR><B>Next:</B> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node13.html"
name=tex2html531>内核模块多文件</A> <B>Up:</B> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node11.html"
name=tex2html527>Hello, world</A> <B>Previous:</B> <A
href="http://yangxingjun.myrice.com/chinesehow/kernel/node11.html"
name=tex2html521>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 + -