old-www/LDP/LG/issue55/tag/5.html

245 lines
10 KiB
HTML

<!--startcut ======================================================= -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<META NAME="generator" CONTENT="lgazmail v1.3D.k">
<TITLE>The Answer Guy 55: More on Exporting Symbols from Shared Libraries</TITLE>
</HEAD><BODY BGCOLOR="#FFFFFF" TEXT="#000000"
LINK="#3366FF" VLINK="#A000A0">
<!-- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -->
<P> <hr>
<!-- *** BEGIN navbar *** :::::::::::::::::::::::::::::::::::::::::::::::: -->
<p align="center">
<A HREF="../lg_bytes55.html"><IMG ALT="[ Prev ]"
SRC="../../gx/navbar/prev.jpg"
WIDTH="16" HEIGHT="45" BORDER="0" ALIGN="bottom"></A>
<IMG ALT="" SRC="../../gx/navbar/left.jpg"
WIDTH="14" HEIGHT="45" BORDER="0" ALIGN="bottom" >
<A HREF="../index.html"><IMG ALT="[ Table of Contents ]"
SRC="../../gx/navbar/toc.jpg"
WIDTH="220" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A>
<A HREF="../../index.html"><IMG ALT="[ Front Page ]"
SRC="../../gx/navbar/frontpage.jpg"
WIDTH="137" HEIGHT="45" BORDER="0" ALIGN="bottom"></A>
<A HREF="../../faq/index.html"><IMG ALT="[ FAQ ]"
SRC="../../gx/navbar/faq.jpg"
WIDTH="62" HEIGHT="45" BORDER="0" ALIGN="bottom"></A>
<IMG ALT="" SRC="../../gx/navbar/right.jpg"
WIDTH="15" HEIGHT="45" ALIGN="bottom" >
<A HREF="../lg_tips55.html"><IMG ALT="[ Next ]"
SRC="../../gx/navbar/next.jpg"
WIDTH="15" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A>
<!-- *** END navbar *** :::::::::::::::::::::::::::::::::::::::::::::::::: -->
</p>
<P> <hr> <P>
<!-- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -->
<center>
<H1><A NAME="answer">
<img src="../../gx/dennis/qbubble.gif" alt="(?)"
border="0" align="middle">
<font color="#B03060">The Answer Guy</font>
<img src="../../gx/dennis/bbubble.gif" alt="(!)"
border="0" align="middle">
</A></H1>
<BR>
<H4>By James T. Dennis,
<a href="mailto:linux-questions-only@ssc.com">linux-questions-only@ssc.com</a><BR>
LinuxCare,
<A HREF="http://www.linuxcare.com/">http://www.linuxcare.com/</A>
</H4>
</center>
<p><hr><p>
<!-- endcut ======================================================= -->
<!-- begin 5 -->
<H3 align="left"><img src="../../gx/dennis/bbubble.gif"
height="50" width="60" alt="(!) " border="0"
>More on Exporting Symbols from Shared Libraries</H3>
<p><strong>Answered By dps on Thu, 08 Jun 2000
</strong></p>
<!-- ::
More on Exporting Symbols from Shared Libraries
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:: -->
<P><STRONG>
There are valid reasons other than those evil one you give for wanting
to limit the exported symbols---prime examples are big jobs split into
multiple source files, which export symbols that implement the feature.
Normally one would not want to export these symbols after linking the
library.
</STRONG></P>
<P><STRONG>
IF anyone wants to use undocumented functions that for advanatge over
the competition then removing it also stops them linking those functions.
A little reading of the binutils man page will reveal, for example the
<TT>-L</TT> optionm in objcopy
</STRONG></P>
<pre><strong> -L symbolname, --localize-symbol=symbolname
Make symbol symbolname local to the file, so that it is
not visible externally. This option may be given more
than once.
</strong></pre>
<P><STRONG>
which seems to fit the bill. (Several symbols in the resolver code
became local in the glibc 2.0 yto glibc 2.1, breaking various programs
that used the undocumented behaviour of those symbols.)
</STRONG></P>
<P><STRONG>
The main differences is that M$ dll's seem to require you to explicitly
list what is externally visible, unlike most unicies and their shared
libraries.
</STRONG></P>
<BLOCKQUOTE><IMG SRC="../../gx/dennis/bbub.gif" ALT="(!)"
HEIGHT="28" WIDTH="50" BORDER="0"
>
I didn't think I characterized it as "evil." I just didn't think
it would be very useful.
</BLOCKQUOTE>
<BLOCKQUOTE>
However, you've shown me a new trick. I hope my earlier
correspondent checks back if he or she still needs this tidbit.
</BLOCKQUOTE>
<!-- end 5 -->
<!-- . . . . . . . . . . . . . . . . . . . -->
<HR WIDTH="40%" ALIGN="center">
<!-- begin 5 -->
<H3 align="left"><img src="../../gx/dennis/bbubble.gif"
height="50" width="60" alt="(!) " border="0"
>Limiting "Public Interfaces" on Share Libraries</H3>
<p><strong>Answered By Steven G. Johnson on Tue, 30 May 2000
</strong></p>
<BLOCKQUOTE>
Hi, noticed your answer regarding "public interfaces" in shared libraries
in the latest Linux Gazette, and I had a couple of comments. (I am a
programmer, and have written several libraries and shared libraries under
Linux.)
</BLOCKQUOTE>
<BLOCKQUOTE>
There are at least two good reasons to hide functions from public interfaces:
</BLOCKQUOTE>
<BLOCKQUOTE><ol>
<li> If a function is internal to the library, and it may well disappear or
change incompatibly without warning in future versions, so that you don't
want to be worry about people using it.
<br>&nbsp;<br>
Any library will almost certainly contain a large number of such internal
functions, and the code would be utterly unmaintainable if you couldn't
change them between releases because people depended on them.
<br>&nbsp;<br>
Of course, it is usually sufficient to simply not document those functions
or declare them in your header files, so that programmers who find out
about them should know that they use them at their own risk. (Some
programmers are foolish enough to do so, even though it is almost never a
good idea. e.g. there was a well-known case where StarOffice had depended
upon internal glibc functions and therefore broke when glibc was upgraded.)
<li> If you don't want to pollute the namespace.
<br>&nbsp;<br>
If I have an internal function in my library called something generic, like
print_error, I run the risk of accidentally conflicting with a function of
the same name in a calling program, with unpredictable results. One way
around this is to prefix the function with the name of my library, calling
it e.g. foo_print_error if my library is libfoo. But this can be awkward
to do for every little internal function you write, and it is often
preferable to simply hide them from the linker.
</ol></BLOCKQUOTE>
<BLOCKQUOTE>
There is a solution, however, provided by ANSI C: simply declare your
functions with the "static" keyword, and they will only be visible/callable
within the file they are defined in. This isn't perfect, I suppose,
because they also aren't visible to other files in the same library.
However, it covers the case where foo_ prefixes are most annoying: little
utility functions that are only called within one file.
</BLOCKQUOTE>
<BLOCKQUOTE>
Cordially,
<br>Steven G. Johnson
</BLOCKQUOTE>
<!-- end 5 -->
<!--startcut ======================================================= -->
<P> <hr> </p>
<H5 align="center"><a href="http://www.linuxgazette.com/copying.html"
>Copyright &copy;</a> 2000, James T. Dennis
<BR>Published in <I>The Linux Gazette</I> Issue 55 July 2000</H5>
<H6 ALIGN="center">HTML transformation by
<A HREF="mailto:star@tuxtops.com">Heather Stern</a> of
Tuxtops, Inc.,
<A HREF="http://www.tuxtops.com/">http://www.tuxtops.com/</A>
</H6>
<P> <hr>
<!-- begin tagnav ::::::::::::::::::::::::::::::::::::::::::::::::::-->
<p align="center">
<table width="100%" border="0"><tr>
<td align="right" valign="center"
><IMG ALT="" SRC="../../gx/navbar/left.jpg"
WIDTH="14" HEIGHT="45" BORDER="0" ALIGN="middle" border="0">
<A HREF="../lg_answer55.html"
><IMG SRC="../../gx/dennis/answertoc.jpg" align="middle"
ALT="[ Answer Guy Current Index ]" border="0"></A></td>
<td align="center" valign="center"><A HREF="../lg_answer55.html#greeting"><img align="middle"
src="../../gx/dennis/smily.gif" alt="greetings" border="0"></A> &nbsp;
<A HREF="1.html">1</A> &nbsp;
<A HREF="2.html">2</A> &nbsp;
<A HREF="3.html">3</A> &nbsp;
<A HREF="4.html">4</A> &nbsp;
<A HREF="5.html">5</A> &nbsp;
<A HREF="6.html">6</A> &nbsp;
<A HREF="7.html">7</A> &nbsp;
<A HREF="8.html">8</A> &nbsp;
<A HREF="9.html">9</A> &nbsp;
<A HREF="10.html">10</A> &nbsp;
<A HREF="11.html">11</A> &nbsp;
<A HREF="12.html">12</A> &nbsp;
<A HREF="13.html">13</A> &nbsp;
<br>
<A HREF="14.html">14</A> &nbsp;
<A HREF="15.html">15</A> &nbsp;
<A HREF="16.html">16</A> &nbsp;
<A HREF="17.html">17</A> &nbsp;
<A HREF="18.html">18</A> &nbsp;
<A HREF="19.html">19</A> &nbsp;
<A HREF="20.html">20</A> &nbsp;
<A HREF="21.html">21</A> &nbsp;
<A HREF="22.html">22</A> &nbsp;
</td>
<td align="left" valign="center"><A HREF="../../tag/kb.html"
><IMG SRC="../../gx/dennis/answerpast.jpg" align="middle"
ALT="[ Index of Past Answers ]" border="0"></A>
<IMG ALT="" SRC="../../gx/navbar/right.jpg" align="middle"
WIDTH="14" HEIGHT="45" BORDER="0"></td></tr></table>
</p>
<!-- end tagnav ::::::::::::::::::::::::::::::::::::::::::::::::::::-->
<P> <hr>
<!-- *** BEGIN navbar *** :::::::::::::::::::::::::::::::::::::::::::::::: -->
<p align="center">
<A HREF="../lg_bytes55.html"><IMG ALT="[ Prev ]"
SRC="../../gx/navbar/prev.jpg"
WIDTH="16" HEIGHT="45" BORDER="0" ALIGN="bottom"></A>
<IMG ALT="" SRC="../../gx/navbar/left.jpg"
WIDTH="14" HEIGHT="45" BORDER="0" ALIGN="bottom" >
<A HREF="../index.html"><IMG ALT="[ Table of Contents ]"
SRC="../../gx/navbar/toc.jpg"
WIDTH="220" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A>
<A HREF="../../index.html"><IMG ALT="[ Front Page ]"
SRC="../../gx/navbar/frontpage.jpg"
WIDTH="137" HEIGHT="45" BORDER="0" ALIGN="bottom"></A>
<A HREF="../../faq/index.html"><IMG ALT="[ FAQ ]"
SRC="../../gx/navbar/faq.jpg"
WIDTH="62" HEIGHT="45" BORDER="0" ALIGN="bottom"></A>
<IMG ALT="" SRC="../../gx/navbar/right.jpg"
WIDTH="15" HEIGHT="45" ALIGN="bottom" >
<A HREF="../lg_tips55.html"><IMG ALT="[ Next ]"
SRC="../../gx/navbar/next.jpg"
WIDTH="15" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A>
<!-- *** END navbar *** :::::::::::::::::::::::::::::::::::::::::::::::::: -->
</p>
<!-- ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: -->
</BODY></HTML>
<!--endcut ========================================================= -->