INTRO(3) | Library Functions Manual | INTRO(3) |
intro
—
introduction to the C libraries
cc |
[flags] file ...
[-llibrary ] |
The manual pages in section 3 provide an overview of the C library
functions, their error returns, and other common definitions and concepts.
Most of these functions are available from the C library,
libc.
Other libraries, such as the math library, libm, must be
indicated at compile time with the -l
option of the
compiler.
The various libraries (followed by the loader flag):
-lLLVM
)-lagentx
)-lc
)-lc
for these functions. There are
several “libraries” or groups of functions included inside
of libc: the standard I/O routines, database routines, bit operators,
string operators, character tests and character operators, cryptographic
routines, storage allocation, time functions, signal handling, and more.
-lc++
)-lc++abi
)-lcbor
)-lcrypto
)-lcurses
)-lncurses
)-lncursesw
)-ltermcap
)-ltermlib
)-lcurses
. See
curses(3) and
termcap(3).
-ledit
)-ledit
-lcurses
. See
editline(3).
-lelf
)-levent
)-lexpat
)-lfido2
)-lform
)-lformw
)-lform
-lcurses
.
libformw is a
hard link to libform intended for use with
libncursesw wide-character functions. See
form(3).
-lfuse
)-lgcc
)-liberty
)-lkeynote
)-lkeynote
-lm
-lcrypto
. See
keynote(3) and
keynote(4).
-lkvm
)-ll
)-lfl
)-lm
)-lmenu
)-lmenu
-lcurses
.
libmenuw is a
hard link to libmenu intended for use with
libncursesw wide-character functions. See
menu(3).
-lossaudio
)-lpanel
)-lpanelw
)-lpanel
-lcurses
.
libpanelw is
a hard link to libpanel intended for use with
libncursesw wide-character functions. See
panel(3).
-lpcap
)-lperl
)-lpthread
)-lradius
)-lreadline
)-lrpcsvc
)-lskey
)-lsndio
)-lssl
)-lstdc++
)-lsupc++
)-ltls
)-lusbhid
)-lutil
)-ly
)-lz
)Platform-specific libraries:
-lalpha
)-lamd64
)-li386
)The system libraries are located in /usr/lib. Typically, a library will have a number of variants:
libc.a libc_p.a libc.so.30.1
Libraries with an ‘.a’ suffix are static. When a
program is linked against a library, all the library code will be linked
into the binary. This means the binary can be run even when the libraries
are unavailable. However, it can be inefficient with memory usage. The C
compiler, cc(1), can be instructed to link
statically by specifying the -static
flag.
Libraries with a ‘_p.a’ suffix are profiling
libraries. They contain extra information suitable for analysing programs,
such as execution speed and call counts. This in turn can be interpreted by
utilities such as gprof(1). The C
compiler, cc(1), can be instructed to
generate profiling code, or to link with profiling libraries, by specifying
the -pg
flag.
Libraries with a ‘.so.X.Y’ suffix are dynamic libraries. When code is compiled dynamically, the library code that the application needs is not linked into the binary. Instead, data structures are added containing information about which dynamic libraries to link with. When the binary is executed, the run-time linker ld.so(1) reads these data structures, and loads them at a virtual address using the mmap(2) system call.
‘X’ represents the major number of the library, and ‘Y’ represents the minor number. In general, a binary will be able to use a dynamic library with a differing minor number, but the major numbers must match. In the example above, a binary linked with minor number ‘3’ would be linkable against libc.so.30.1, while a binary linked with major number ‘31’ would not.
The advantages of dynamic libraries are that multiple instances of
the same program can share address space, and the physical size of the
binary is smaller. The disadvantage is the added complexity that comes with
loading the libraries dynamically, and the extra time taken to load the
libraries. Of course, if the libraries are not available, the binary will be
unable to execute. The C compiler, cc(1), can
be instructed to link dynamically by specifying the
-shared
flag, although on systems that support it,
this will be the default and need not be specified.
Shared libraries, as well as static libraries on architectures
which produce position-independent executables (PIEs) by default, contain
position-independent code (PIC). Normally, compilers produce relocatable
code. Relocatable code needs to be modified at run-time, depending on where
in memory it is to be run. PIC code does not need to be modified at
run-time, but is less efficient than relocatable code. The C compiler,
cc(1), can be instructed to generate PIC code
by specifying the -fpic
or
-fPIC
flags.
With the exception of dynamic libraries, libraries are generated using the ar(1) utility. The libraries contain an index to the contents of the library, stored within the library itself. The index lists each symbol defined by a member of a library that is a relocatable object file. This speeds up linking to the library, and allows routines in the library to call each other regardless of their placement within the library. The index is created by ranlib(1) and can be viewed using nm(1).
The building of dynamic libraries can be prevented by setting the
variable NOPIC
in
/etc/mk.conf. The building of profiling versions of
libraries can be prevented by setting the variable
NOPROFILE
in /etc/mk.conf.
See mk.conf(5) for more details.
ar(1), cc(1), gprof(1), ld(1), ld.so(1), nm(1), ranlib(1), mk.conf(5)
An intro
manual for section 3 first
appeared in Version 7 AT&T UNIX.
October 26, 2020 | OpenBSD-current |