Libraries |
The Difference Between Header Files and Libraries |
Header Files |
.h
". The standard header files
are located in directory /usr/include
.
Standard header files are included in C programs
with lines such as:
#include <stdio.h>
#include <stdlib.h>
#include "my_header.h"
To have the compiler search in (nonstandard) directories for
header file use the include directory option
-I
, which may appear several times:
cc options -Imyheaderdir -Ianotherheaderdir
options
/usr/include
is searched by default and does not need to be specified.
Libraries |
The linker needs to be told to link libraries and may need to
be told where those libraries are to be found.
This is usually done on the compile
command. Note that this is distinct and different from specifying
header files (-I
options).
The relevant options are
-L
and
-l
.
cc options -Lmydir
options -lmine -lm
mydir
,
as well as, the usual place(s) such as /usr/lib
.
The libraries to search for are
libmine.a
or
libmine.so
as appropriate, as well as, the math library
libm.a
or
libm.so
as appropriate (see shared libraries below).
Note that a prefix lib
and a suffix .a
or .so
are appended to the name specified
on the command line.
The standard C library
libc.a
or
libc.so
is automatically linked last
and does not need to be specified. The order and location
of the -l
options is important.
Since object and library files given on
the command line are examined left to right, libraries should be given last
(on the right); code from a library is used only if it resolves references
in files examined up to that point in the command line.
Some libraries that may need to be linked in with an -l option are:
m
(math library),
X11
(X 11),
pthreads
(POSIX threads library),
and various networking libraries (for example, xnet
on Solaris 2.6 or
nsl
,
socket
on older
Solaris systems).
Note that the compiler automatically asks the linker to link in a
startup routine (as if it had been added as the first argument).
Typically crt0.o
or something similar is used, but the
startup routine may vary. For example,
with the debugging option (-g
)
or profiling options, many compilers will use different startup
routines, such as gcrt1.o
,
mcrt1.0
or what have you.
A simple compile command such as cc hello.c
on many systems
will result in a linker (loader) command such as
ld /lib/crt0.o hello.o -lc
which uses library
/usr/lib/libc.a
.
Static versus Dynamic Libraries |
Dynamically linked libraries are significantly better than static libraries and should be used whenever possible for the following reasons:
Statically compiled programs are appropriate only if there is a security concern about changes to libraries or for executables that need to be runnable even in the event of system problems that prevent libraries from loading. Root's shell is usually statically linked for this reason. Static binaries are often located in directory /sbin.
Static libraries typically have the extension ".a
", whereas
dynamically linked libraries have extension ".so
".
Using Shared Libraries on Solaris |
LD_LIBRARY_PATH
specifies the search path for shared libraries.
Typically it will contain
/usr/openwin/lib
and
perhaps a compiler and a database library directory.
A program can be compiled with option -R
(or the variable
LD_RUN_PATH
can be set) which records in the executable
the names of shared library directories to search;
this way users do not need to change their
LD_LIBRARY_PATH
to include new search directories.
The Solaris linker searches for libraries in
directories given in LD_LIBRARY_PATH
(as if these had been given with
a -L
option).
The system call dlopen( )
can be used by programs
that wish to handle libraries directly.
Useful Utilities |
ldd - show shared library dependencies
Generating Shared Libraries |
cc
-G
-o libmine.so file1.o ...
-KPIC
to make code position-independent.
cc
-shared
-o libmine.so file1.o ...
libmine.so
is the name of the shared library and
the remaining files are the object files to put in the library.
Generating Static Libraries |
ar r libmine.a file1.o ...
ranlib libmine.a ## needed on BSD systems only
libmine.a
is the name of the static library.
Since these command usually appear in a Makefile and many makes
define RANLIB
if
ranlib
is needed, good commands for Makefiles are:
libmine.a: file1.o ...
$(AR) r $@ $?
$(RANLIB) $@
RANLIB=-@true
in your environment
(.profile
or
.login
file) for the majority of systems
that do not use ranlib
.
Last update: 2001 April 28