JezK
Edit File: rtl.define.library.html
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>What is a C library?</title><link rel="stylesheet" type="text/css" href="userguide.css" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="home" href="index.html" title="FreeTDS User Guide" /><link rel="up" href="rtl.html" title="Appendix A. On Linkers" /><link rel="prev" href="rtl.html" title="Appendix A. On Linkers" /><link rel="next" href="linker.library.check.html" title="Checking if a Library Provides a Function" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">What is a C library?</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="rtl.html">Prev</a> </td><th width="60%" align="center">Appendix A. On Linkers</th><td width="20%" align="right"> <a accesskey="n" href="linker.library.check.html">Next</a></td></tr></table><hr /></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="rtl.define.library"></a>What is a C library?</h2></div></div></div><p>A C library is a set of named functions, for example <code class="literal">dbinit()</code> or <code class="literal">SQLConnect()</code>. Or, for that matter, <code class="literal">fopen(3)</code><a href="#ftn.idm6590" class="footnote" id="idm6590"><sup class="footnote">[34]</sup></a>. </p><p>Libraries come in two flavors: <em class="firstterm">static</em> and <em class="firstterm">dynamic</em>. </p><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="rtl.define.library.static"></a>Static libraries</h3></div></div></div><p>Static libraries (also known as <em class="firstterm">archives</em>) have been around as long as C itself. Like a <code class="literal">.zip</code> file, they're just a bag of object files — containing functions, of course — with a table of contents in front giving the address of each name<a href="#ftn.idm6601" class="footnote" id="idm6601"><sup class="footnote">[35]</sup></a>. Static libraries are created from object files using a <em class="firstterm">librarian</em> utility of some kind. One such programs is <span class="command"><strong>ar</strong></span>, for <span class="emphasis"><em>archive</em></span>. </p><p>Static libraries are part of the build environment. Functions in static libraries are joined to a program's main module by a <em class="firstterm">static linker</em> at build time to produce an executable program. The executable incorporates the libraries' object code into its own body, making it completely self-sufficient. </p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="rtl.define.library.dynamic"></a>Dynamic libraries</h3></div></div></div><p>Dynamic libraries are the new kid on the block, as these things go, arriving on the Unix scene circa 1985. Like a static library, a dynamic library is a collection of functions with a table of contents. They are referenced at build time to give the executatble information about how they will eventually be used, but they aren't <span class="emphasis"><em>used</em></span> until run time. </p><p>Dynamic libraries are part of the run-time environment. When a program is run, the run-time linker finds the dynamic libraries needed by the program, finds the addresses of the required functions, and assembles a runable image in memory. Missing libraries and/or missing functions — or the wrong versions of them — can lead to head-scratching and other amusing behavior. </p><p>In Windows® dynamic libraries are called <em class="firstterm">dynamic link libraries</em> (DLLs). In Unix they're normally called <em class="firstterm">shared objects</em>. But they're roughly the same thing. </p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note: What about .h files?"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../images/note.gif" /></td><th align="left">What about <code class="literal">.h</code> files?</th></tr><tr><td align="left" valign="top"><p>C header files include <em class="firstterm">functional prototypes</em>, declarations (not <span class="emphasis"><em>definitions)</em></span> of functions. Functional prototypes describe to the compiler each function's parameters, allowing the compiler to confirm that the function is being called correctly. </p><p>Most of the functions declared in header files are implemented in libraries. However, there's <span class="emphasis"><em>no mechanical or automatic relationship</em></span> between the functional prototypes in the header files and their implementation in a library. The <code class="literal">.h</code> file is maintained by hand, by the programmer, and is used to generate a library. The header file and associated library are distributed and installed together (one hopes), but correct installation and subsequent use by the compiler & linker require human beings to keep track of the pair. Failure to do so leads to <span class="quote">“<span class="quote">interesting</span>”</span> development and even run-time problems, especially with libraries whose functions' parameters change from version to version. </p><p>For example, imagine a function <code class="literal">f(int g)</code> defined in library <code class="filename">libf.so</code> and declared in <code class="filename">f.h</code>. In a later version of <code class="filename">libf.so</code>, the function's parameter is changed to use a pointer, <code class="literal">f(int *p)</code>, and <code class="filename">f.h</code> is likewise updated. Possible errors that cannot be prevented by the linker include: </p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"><p>An old program could use the new library. Probably the integer it passes will be interpreted as an out-of-bounds address, resulting in a segmentation violation. </p></li><li class="listitem"><p>A new program could use the old library, passing an address that the library interprets as an integer. Hillarity ensues. </p></li><li class="listitem"><p>Existing source code could be compiled using the old header file but linked to the new library. If you've never done that, give it time. </p></li></ol></div><p> These errors are possible because C functions are identified to the linker <span class="emphasis"><em>by name only</em></span>. On the upside, that makes the tools simple and easy to implement and, by the same token, simplifies the use of C libraries by other languages. The downside is that the work of ensuring that the right libraries are used becomes an administrative task instead of a technical one. </p></td></tr></table></div></div><div class="footnotes"><br /><hr style="width:100; text-align:left;margin-left: 0" /><div id="ftn.idm6590" class="footnote"><p><a href="#idm6590" class="para"><sup class="para">[34] </sup></a>The Unix convention is to put in parentheses behind the name the section of the manual in which the function is documented. <span class="productname">FreeTDS</span> functions don't get numbers because they're not in the manual. Yet. </p></div><div id="ftn.idm6601" class="footnote"><p><a href="#idm6601" class="para"><sup class="para">[35] </sup></a>Or, depending on how you look at it, the name of each address.</p></div></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="rtl.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="rtl.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="linker.library.check.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Appendix A. On Linkers </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Checking if a Library Provides a Function</td></tr></table></div></body></html>