📄 autobook_162.html
字号:
<HTML><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><!-- Created on September, 12 2004 by texi2html 1.64 --><!-- Written by: Lionel Cons <Lionel.Cons@cern.ch> (original author) Karl Berry <karl@freefriends.org> Olaf Bachmann <obachman@mathematik.uni-kl.de> and many others.Maintained by: Olaf Bachmann <obachman@mathematik.uni-kl.de>Send bugs and suggestions to <texi2html@mathematik.uni-kl.de> --><HEAD><TITLE>Autoconf, Automake, and Libtool: A Simple GNU/Linux Module Loader</TITLE><META NAME="description" CONTENT="Autoconf, Automake, and Libtool: A Simple GNU/Linux Module Loader"><META NAME="keywords" CONTENT="Autoconf, Automake, and Libtool: A Simple GNU/Linux Module Loader"><META NAME="resource-type" CONTENT="document"><META NAME="distribution" CONTENT="global"><META NAME="Generator" CONTENT="texi2html 1.64"><script language="Javascript"><!-- // Check the browser version. function checkVersion() { if (navigator.appVersion.charAt(0)>=3) return true; if (navigator.appVersion.charAt(0)>=4) return true; else return false; } if (checkVersion()) { homeon = new Image(); homeon.src = "homeon.png"; homeoff = new Image(); homeoff.src = "home.png"; tocon = new Image(); tocon.src = "tocon.png"; tocoff = new Image(); tocoff.src = "toc.png"; indexon = new Image(); indexon.src = "indexon.png"; indexoff = new Image(); indexoff.src = "index.png"; helpon = new Image(); helpon.src = "helpon.png"; helpoff = new Image(); helpoff.src = "help.png"; backon = new Image(); backon.src = "backon.png"; backoff = new Image(); backoff.src = "back.png"; forwardon = new Image(); forwardon.src = "forwardon.png"; forwardoff = new Image(); forwardoff.src = "forward.png"; prevon = new Image(); prevon.src = "prevon.png"; prevoff = new Image(); prevoff.src = "prev.png"; nexton = new Image(); nexton.src = "nexton.png"; nextoff = new Image(); nextoff.src = "next.png"; upon = new Image(); upon.src = "upon.png"; upoff = new Image(); upoff.src = "up.png"; } function img_act(imgName) { if (checkVersion()) { imgOn = eval(imgName + "on.src"); document [imgName].src = imgOn; } } function img_inact(imgName) { if (checkVersion()) { imgOff = eval(imgName + "off.src"); document [imgName].src = imgOff; } }// --></SCRIPT></HEAD><BODY LANG="EN" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#6688AA" VLINK="#336688" ALINK="#808080"><A NAME="SEC162"></A><TABLE BORDER=0 CELLPADDING=0 CELLSPACING=10><TR VALIGN="TOP"><TD ALIGN="MIDDLE" WIDTH=50 BGCOLOR="#e6e6e6"><TABLE CELLPADDING=1 CELLSPACING=1 BORDER=0><TR VALIGN="TOP" ALIGN="LEFT"><TD VALIGN="MIDDLE" ALIGN="LEFT"><A HREF="autobook_161.html#SEC161" onMouseover="img_act('prev')" onMouseout="img_inact('prev')"><IMG SRC="prev.png" BORDER="0" ALT="Back: Finding a Module" ALIGN="MIDDLE" NAME="prev"></A></TD></TR><TR VALIGN="TOP" ALIGN="LEFT"><TD VALIGN="MIDDLE" ALIGN="LEFT"><A HREF="autobook_163.html#SEC163" onMouseover="img_act('next')" onMouseout="img_inact('next')"><IMG SRC="next.png" BORDER="0" ALT="Forward: A Simple GNU/Linux Dynamic Module" ALIGN="MIDDLE" NAME="next"></A></TD></TR><TR VALIGN="TOP" ALIGN="LEFT"><TD VALIGN="MIDDLE" ALIGN="LEFT"> <TR VALIGN="TOP" ALIGN="LEFT"><TD VALIGN="MIDDLE" ALIGN="LEFT"><A HREF="autobook_163.html#SEC163" onMouseover="img_act('back')" onMouseout="img_inact('back')"><IMG SRC="back.png" BORDER="0" ALT="FastBack: A Simple GNU/Linux Dynamic Module" ALIGN="MIDDLE" NAME="back"></A></TD></TR><TR VALIGN="TOP" ALIGN="LEFT"><TD VALIGN="MIDDLE" ALIGN="LEFT"><A HREF="autobook_158.html#SEC158" onMouseover="img_act('up')" onMouseout="img_inact('up')"><IMG SRC="up.png" BORDER="0" ALT="Up: Dynamic Loading" ALIGN="MIDDLE" NAME="up"></A></TD></TR><TR VALIGN="TOP" ALIGN="LEFT"><TD VALIGN="MIDDLE" ALIGN="LEFT"><A HREF="autobook_164.html#SEC164" onMouseover="img_act('forward')" onMouseout="img_inact('forward')"><IMG SRC="forward.png" BORDER="0" ALT="FastForward: Using GNU libltdl" ALIGN="MIDDLE" NAME="forward"></A></TD></TR><TR VALIGN="TOP" ALIGN="LEFT"><TD VALIGN="MIDDLE" ALIGN="LEFT"><A HREF="autobook.html#SEC_Top" onMouseover="img_act('home')" onMouseout="img_inact('home')"><IMG SRC="home.png" BORDER="0" ALT="Top: Autoconf, Automake, and Libtool" ALIGN="MIDDLE" NAME="home"></A></TD></TR><TR VALIGN="TOP" ALIGN="LEFT"><TD VALIGN="MIDDLE" ALIGN="LEFT"><A HREF="autobook_toc.html#SEC_Contents" onMouseover="img_act('toc')" onMouseout="img_inact('toc')"><IMG SRC="toc.png" BORDER="0" ALT="Contents: Table of Contents" ALIGN="MIDDLE" NAME="toc"></A></TD></TR><TR VALIGN="TOP" ALIGN="LEFT"><TD VALIGN="MIDDLE" ALIGN="LEFT"><A HREF="autobook_285.html#SEC285" onMouseover="img_act('index')" onMouseout="img_inact('index')"><IMG SRC="index.png" BORDER="0" ALT="Index: Index" ALIGN="MIDDLE" NAME="index"></A></TD></TR><TR VALIGN="TOP" ALIGN="LEFT"><TD VALIGN="MIDDLE" ALIGN="LEFT"><A HREF="autobook_abt.html#SEC_About" onMouseover="img_act('help')" onMouseout="img_inact('help')"><IMG SRC="help.png" BORDER="0" ALT="About: About this document" ALIGN="MIDDLE" NAME="help"></A></TD></TR></TABLE></TD><TD ALIGN="LEFT"><H2> 17.4 A Simple GNU/Linux Module Loader </H2><!--docid::SEC162::--><P>Something to be aware of, is that when your users write dynamic modulesfor your application, they are subject to the interface you design. Itis very important to design a dynamic module interface that is clean andfunctional before other people start to write modules for your code. Ifyou ever need to change the interface, your users will need to rewritetheir modules. Of course you can carefully change the interface toretain backwards compatibility to save your users the trouble ofrewriting their modules, but that is no substitute for designing agood interface from the outset. If you do get it wrong, andsubsequently discover that the design you implemented is misconceived(this is the voice of experience speaking!), you will be left with adifficult choice: try to tweak the broken API so that it does workwhile retaining backwards compatibility, and the maintenance andperformance penalty that brings? Or start again with a fresh design bornof the experience gained last time, and rewrite all of the modules youhave so far?</P><P>If there are other applications which have similar module requirementsto you, it is worth writing a loader that uses the same interface andsemantics. That way, you will (hopefully) be building from a known goodAPI design, and you will have access to all the modules for thatother application too, and vice versa. </P><P>For the sake of clarity, I have sidestepped any issues of APIdesign for the following example, by choosing this minimal interface:</P><P><A NAME="IDX26"></A><DL><DT><U>Function:</U> int <B>run</B> <I>(const char *<VAR>argument</VAR>)</I><DD>When the module is successfully loaded a function with the followingprototype is called with the argument given on the command line. Ifthis entry point is found and called, but returns <SAMP>`-1'</SAMP>, an errormessage is displayed by the calling program.</DL></P><P>Here's a simplistic but complete dynamic module loading application youcan build for this interface with the GNU/Linux dynamic loadingAPI:</P><P><TABLE width=100%><tr><td> </td><td class=example bgcolor=#6688aa><br><pre></pre></td></tr></table></P><P>You would compile this on a GNU/Linux machine like so:</P><P><TABLE width=100%><tr><td> </td><td class=example bgcolor=#6688aa><br><pre>$ gcc -o simple-loader simple-loader.c -ldl</pre></td></tr></table></P><P>However, despite making reasonable effort with this loader, and ignoringfeatures which could easily be added, it still has some seeminglyinsoluble problems:</P><P><OL><LI>It will fail if the user's platform doesn't have the <CODE>dlopen</CODE>API. This also includes platforms which have no shared libraries.<P><LI>It relies on the implementation to provide a working self-openingmechanism. <SAMP>`dlopen (NULL, RTLD_NOW)'</SAMP> is very often unimplemented,or buggy, and without that, it is impossible to access the symbols ofthe main program through the <SAMP>`dlsym'</SAMP> mechanism.<P><LI>It is quite difficult to figure out at compile time whether the targethost needs <TT>`libdl.so'</TT> to be linked.</OL><P>I will use GNU Autotools to tackle these problems in the next chapter.</P><P><A NAME="A Simple GNU/Linux Dynamic Module"></A></TR></TABLE><BR> <FONT SIZE="-1">This document was generatedby <I>Gary V. Vaughan</I> on <I>September, 12 2004</I>using <A HREF="http://www.mathematik.uni-kl.de/~obachman/Texi2html"><I>texi2html</I></A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -