197 lines
4.2 KiB
HTML
197 lines
4.2 KiB
HTML
<HTML
|
|
><HEAD
|
|
><TITLE
|
|
>Static Libraries</TITLE
|
|
><META
|
|
NAME="GENERATOR"
|
|
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
|
|
REL="HOME"
|
|
TITLE="Program Library HOWTO"
|
|
HREF="index.html"><LINK
|
|
REL="PREVIOUS"
|
|
TITLE="Introduction"
|
|
HREF="introduction.html"><LINK
|
|
REL="NEXT"
|
|
TITLE="Shared Libraries"
|
|
HREF="shared-libraries.html"></HEAD
|
|
><BODY
|
|
CLASS="SECT1"
|
|
BGCOLOR="#FFFFFF"
|
|
TEXT="#000000"
|
|
LINK="#0000FF"
|
|
VLINK="#840084"
|
|
ALINK="#0000FF"
|
|
><DIV
|
|
CLASS="NAVHEADER"
|
|
><TABLE
|
|
SUMMARY="Header navigation table"
|
|
WIDTH="100%"
|
|
BORDER="0"
|
|
CELLPADDING="0"
|
|
CELLSPACING="0"
|
|
><TR
|
|
><TH
|
|
COLSPAN="3"
|
|
ALIGN="center"
|
|
>Program Library HOWTO</TH
|
|
></TR
|
|
><TR
|
|
><TD
|
|
WIDTH="10%"
|
|
ALIGN="left"
|
|
VALIGN="bottom"
|
|
><A
|
|
HREF="introduction.html"
|
|
ACCESSKEY="P"
|
|
>Prev</A
|
|
></TD
|
|
><TD
|
|
WIDTH="80%"
|
|
ALIGN="center"
|
|
VALIGN="bottom"
|
|
></TD
|
|
><TD
|
|
WIDTH="10%"
|
|
ALIGN="right"
|
|
VALIGN="bottom"
|
|
><A
|
|
HREF="shared-libraries.html"
|
|
ACCESSKEY="N"
|
|
>Next</A
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
><HR
|
|
ALIGN="LEFT"
|
|
WIDTH="100%"></DIV
|
|
><DIV
|
|
CLASS="SECT1"
|
|
><H1
|
|
CLASS="SECT1"
|
|
><A
|
|
NAME="STATIC-LIBRARIES"
|
|
></A
|
|
>2. Static Libraries</H1
|
|
><P
|
|
>Static libraries are simply a collection of ordinary object files;
|
|
conventionally, static libraries end with the ``.a'' suffix.
|
|
This collection is created using the ar (archiver) program.
|
|
Static libraries aren't used as often as they once were, because of the
|
|
advantages of shared libraries (described below).
|
|
Still, they're sometimes created, they existed first historically,
|
|
and they're simpler to explain.</P
|
|
><P
|
|
>Static libraries permit users to link to programs without having to
|
|
recompile its code, saving recompilation time.
|
|
Note that recompilation time is less important given today's faster compilers,
|
|
so this reason is not as strong as it once was.
|
|
Static libraries are often useful for developers if they wish to
|
|
permit programmers to link to their library, but don't want to give
|
|
the library source code (which is an advantage to the library vendor, but
|
|
obviously not an advantage to the programmer trying to use the library).
|
|
In theory, code in static ELF libraries that is linked into an
|
|
executable should run slightly faster (by 1-5%) than a shared library
|
|
or a dynamically loaded library, but in practice this
|
|
rarely seems to be the case due to other confounding factors.</P
|
|
><P
|
|
>To create a static library, or to add additional object files to
|
|
an existing static library, use a command like this:</P
|
|
><TABLE
|
|
BORDER="0"
|
|
BGCOLOR="#E0E0E0"
|
|
WIDTH="100%"
|
|
><TR
|
|
><TD
|
|
><FONT
|
|
COLOR="#000000"
|
|
><PRE
|
|
CLASS="PROGRAMLISTING"
|
|
>ar rcs my_library.a file1.o file2.o</PRE
|
|
></FONT
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
><P
|
|
>This sample command adds the object files file1.o and file2.o to the
|
|
static library my_library.a, creating my_library.a if it doesn't
|
|
already exist.
|
|
For more information on creating static libraries, see ar(1).</P
|
|
><P
|
|
>Once you've created a static library, you'll want to use it.
|
|
You can use a static library by invoking it as part of the compilation
|
|
and linking process when creating a program executable.
|
|
If you're using gcc(1) to generate your executable, you can use the -l
|
|
option to specify the library; see info:gcc for more information.</P
|
|
><P
|
|
>Be careful about the order of the parameters when using gcc;
|
|
the -l option is a linker option, and thus needs to be placed
|
|
AFTER the name of the file to be compiled.
|
|
This is quite different from the normal option syntax.
|
|
If you place the -l option before the filename, it may fail to link at all,
|
|
and you can end up with mysterious errors.</P
|
|
><P
|
|
>You can also use the linker ld(1) directly, using its -l and -L options;
|
|
however, in most cases it's better to use gcc(1) since the interface of
|
|
ld(1) is more likely to change.</P
|
|
></DIV
|
|
><DIV
|
|
CLASS="NAVFOOTER"
|
|
><HR
|
|
ALIGN="LEFT"
|
|
WIDTH="100%"><TABLE
|
|
SUMMARY="Footer navigation table"
|
|
WIDTH="100%"
|
|
BORDER="0"
|
|
CELLPADDING="0"
|
|
CELLSPACING="0"
|
|
><TR
|
|
><TD
|
|
WIDTH="33%"
|
|
ALIGN="left"
|
|
VALIGN="top"
|
|
><A
|
|
HREF="introduction.html"
|
|
ACCESSKEY="P"
|
|
>Prev</A
|
|
></TD
|
|
><TD
|
|
WIDTH="34%"
|
|
ALIGN="center"
|
|
VALIGN="top"
|
|
><A
|
|
HREF="index.html"
|
|
ACCESSKEY="H"
|
|
>Home</A
|
|
></TD
|
|
><TD
|
|
WIDTH="33%"
|
|
ALIGN="right"
|
|
VALIGN="top"
|
|
><A
|
|
HREF="shared-libraries.html"
|
|
ACCESSKEY="N"
|
|
>Next</A
|
|
></TD
|
|
></TR
|
|
><TR
|
|
><TD
|
|
WIDTH="33%"
|
|
ALIGN="left"
|
|
VALIGN="top"
|
|
>Introduction</TD
|
|
><TD
|
|
WIDTH="34%"
|
|
ALIGN="center"
|
|
VALIGN="top"
|
|
> </TD
|
|
><TD
|
|
WIDTH="33%"
|
|
ALIGN="right"
|
|
VALIGN="top"
|
|
>Shared Libraries</TD
|
|
></TR
|
|
></TABLE
|
|
></DIV
|
|
></BODY
|
|
></HTML
|
|
> |