xref: /freebsd/share/man/man3/intro.3 (revision 02e9120893770924227138ba49df1edb3896112a)
1.\" Copyright (c) 1980, 1991, 1993
2.\"	The Regents of the University of California.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\" 3. Neither the name of the University nor the names of its contributors
13.\"    may be used to endorse or promote products derived from this software
14.\"    without specific prior written permission.
15.\"
16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE.
27.Dd November 16, 2023
28.Dt INTRO 3
29.Os
30.Sh NAME
31.Nm intro
32.Nd introduction to the C libraries
33.Sh SYNOPSIS
34.Nm cc
35.Op Ar flags
36.Ar
37.Op Fl llibrary
38.Sh DESCRIPTION
39This section provides an overview of the C
40library functions, their error returns and other
41common definitions and concepts.
42Most of these functions are available from the C library,
43.Em libc .
44Other libraries, such as the math library,
45.Em libm ,
46must be indicated at compile time with the
47.Fl l
48option of the compiler.
49.Pp
50The various libraries (followed by the loader flag):
51.Bl -tag -width "libbluetooth"
52.It Em libbluetooth
53.Pq Fl l Ns Ar bluetooth
54The bluetooth library.
55See
56.Xr bluetooth 3 .
57.It Em libc
58.Pq Fl l Ns Ar c
59Standard C library functions.
60When using the C compiler
61.Xr cc 1 ,
62it is not necessary
63to supply the loader flag
64.Fl l Ns Ar c
65for these functions.
66There are several `libraries' or groups of functions included inside of
67.Em libc :
68.Bl -tag -width "XXXXXX"
69.It standard I/O routines
70see
71.Xr stdio 3
72.It database routines
73see
74.Xr db 3
75.It bit operators
76see
77.Xr bitstring 3
78.It string operators
79see
80.Xr string 3
81.It character tests and character operators
82.It storage allocation
83see
84.Xr mpool 3
85.It regular-expressions
86see
87.Xr regex 3
88.It remote procedure calls (RPC)
89see
90.Xr rpc 3
91.It time functions
92see
93.Xr time 3
94.It signal handling
95see
96.Xr signal 3
97.El
98.It Em libcalendar
99.Pq Fl l Ns Ar calendar
100The calendar arithmetic library.
101See
102.Xr calendar 3 .
103.It Em libcam
104.Pq Fl l Ns Ar cam
105The common access method user library.
106See
107.Xr cam 3 .
108.It Em libcrypt
109.Pq Fl l Ns Ar crypt
110The crypt library.
111See
112.Xr crypt 3 .
113.It Em libcurses
114.Pq Fl l Ns Ar curses Fl l Ns Ar termcap
115Terminal independent screen management routines
116for two dimensional non-bitmap display terminals.
117See
118.Xr ncurses 3 .
119.It Em libcuse
120.Pq Fl l Ns Ar cuse
121The userland character device library.
122See
123.Xr cuse 3 .
124.It Em libcompat
125.Pq Fl l Ns Ar compat
126Functions which are obsolete but are available for compatibility with
127.Bx 4.3 .
128In particular,
129a number of system call interfaces provided in previous releases of
130.Bx
131have been included for source code compatibility.
132Use of these routines should, for the most part, be avoided.
133The manual page entry for each compatibility routine
134indicates the proper interface to use.
135.It Em libdevinfo
136.Pq Fl l Ns Ar devinfo
137The Device and Resource Information Utility library.
138See
139.Xr devinfo 3 .
140.It Em libdevstat
141.Pq Fl l Ns Ar devstat
142The Device Statistics library.
143See
144.Xr devstat 3 .
145.It Em libdwarf
146.Pq Fl l Ns Ar dwarf
147The DWARF access library.
148See
149.Xr dwarf 3 .
150.It Em libelf
151.Pq Fl l Ns Ar elf
152The ELF access library.
153See
154.Xr elf 3 .
155.It Em libfetch
156.Pq Fl l Ns Ar fetch
157The file transfer library.
158See
159.Xr fetch 3 .
160.It Em libfigpar
161.Pq Fl l Ns Ar figpar
162The configuration file parsing library.
163See
164.Xr figpar 3 .
165.It Em libgpio
166.Pq Fl l Ns Ar gpio
167The general-purpose input output library (GPIO).
168See
169.Xr gpio 3 .
170.It Em libgssapi
171.Pq Fl l Ns Ar gssapi
172The generic security service application programming
173interface.
174See
175.Xr gssapi 3 .
176.It Em libjail
177.Pq Fl l Ns Ar jail
178The jail library.
179See
180.Xr jail 3 .
181.It Em libkvm
182.Pq Fl l Ns Ar kvm
183Functions used to access kernel memory are in this library.
184They can be used
185against both a running system and a crash dump.
186See
187.Xr kvm 3 .
188.It Em libl
189.Pq Fl l Ns Ar l
190The library for
191.Xr lex 1 .
192.It Em libm
193.Pq Fl l Ns Ar m
194The math library.
195See
196.Xr math 3 .
197.It Em libmd
198.Pq Fl l Ns Ar md
199The message digest library.
200See
201.Xr md4 3 ,
202.Xr md5 3 ,
203.Xr sha 3 ,
204.Xr sha256 3 ,
205.Xr sha512 3 ,
206.Xr ripemd 3 ,
207.Xr skein 3 .
208.It Em libmp
209.Pq Fl l Ns Ar mp
210.It Em libpam
211.Pq Fl l Ns Ar pam
212The pluggable authentication module library.
213See
214.Xr pam 3 .
215.It Em libpcap
216.Pq Fl l Ns Ar pcap
217The packet capture library.
218See
219.Xr pcap 3 .
220.It Em libpmc
221.Pq Fl l Ns Ar pmc
222The performance counters library.
223See
224.Xr pmc 3 .
225.It Em libpthread
226.Pq Fl l Ns Ar pthread
227The POSIX threads library.
228See
229.Xr pthread 3 .
230.It Em libstdthreads
231.Pq Fl l Ns Ar stdthreads
232The ISO C11 standard
233.In threads.h
234library.
235See
236.Xr thrd_create 3 .
237.It Em libsysdecode
238.Pq Fl l Ns Ar sysdecode
239The system argument decoding library.
240See
241.Xr sysdecode 3 .
242.It Em libtermcap
243.Pq Fl l Ns Ar termcap
244The terminal independent operation library package.
245See
246.Xr termcap 3 .
247.It Em libusb
248.Pq Fl l Ns Ar usb
249The USB access library.
250See
251.Xr usb 3 .
252.It Em libvgl
253.Pq Fl l Ns Ar vgl
254The video graphics library.
255See
256.Xr vgl 3 .
257.It Em liby
258.Pq Fl l Ns Ar y
259The library for
260.Xr yacc 1 .
261.It Em libz
262.Pq Fl l Ns Ar z
263The general-purpose data compression library.
264See
265.Xr zlib 3 .
266.El
267.Sh FILES
268.Bl -tag -width /usr/lib/libm_p.a -compact
269.It Pa /usr/lib/libc.a
270the C library
271.It Pa /usr/lib/libc_p.a
272the C library compiled for profiling
273.It Pa /usr/lib/libm.a
274the math library
275.It Pa /usr/lib/libm_p.a
276the math library compiled for profiling
277.El
278.Sh LIBRARY TYPES
279The system libraries are located in
280.Pa /lib
281and
282.Pa /usr/lib .
283A library has the following naming convention:
284.Bd -unfilled -offset indent
285libc.so.7
286.Ed
287.Pp
288Libraries with an
289.Sq .a
290suffix are static.
291When a program is linked against a static library, all necessary library code
292will be included in the binary.
293This means the binary can be run even when the libraries are unavailable.
294However, it can be inefficient with both disk space and memory usage
295during execution.
296The C compiler,
297.Xr cc 1 ,
298can be instructed to link statically by specifying the
299.Fl static
300flag.
301.Pp
302Libraries with a
303.Sq .so.X
304suffix are dynamic libraries.
305When code is linked dynamically, the library code that the application needs
306is not included in the binary.
307Instead, data structures are added containing information about which dynamic
308libraries to link with.
309When the binary is executed, the run-time linker
310.Xr ld.so 1
311reads these data structures and loads them into the
312process virtual address space.
313.Xr rtld 1
314loads the shared libraries when the program is executed.
315.Pp
316.Sq X
317represents the library version number of the library.
318In the example above, a binary linked with
319.Pa libc.so.8
320would not be usable on a system where only
321.Pa libc.so.7
322is available.
323.Pp
324The advantages of dynamic libraries are that multiple instances of the same
325library can share address space, and the physical size of the binary is
326smaller.
327A namespace per shared library is available via hidden visibility,
328allowing multiple compilation units in a library to share things without
329making them available to other libraries.
330It is possible to load libraries dynamically via
331.Xr dlopen 3 .
332The disadvantage is the added complexity that comes with loading the
333libraries dynamically, and the extra time taken to load the libraries.
334Of course, if the libraries are not available, the binary will be unable
335to execute.
336Calls across shared libraries are also slightly slower and cannot be
337inlined, not even with link time optimization.
338The C compiler,
339.Xr cc 1 ,
340can be instructed to link dynamically by specifying the
341.Fl shared
342flag.
343.Pp
344Shared libraries, as well as static libraries on architectures which produce
345position-independent executables
346.Pq PIEs
347by default, contain position-independent code
348.Pq PIC .
349Normally, compilers produce relocatable code.
350Relocatable code needs to be modified at run-time, depending on where in
351memory it is to be run.
352The C compiler,
353.Xr cc 1 ,
354can be instructed to generate PIC code by specifying the
355.Fl fPIC
356flag.
357.Pp
358Static libraries are generated using the
359.Xr ar 1
360utility.
361The libraries contain an index to the contents of the library,
362stored within the library itself.
363The index lists each symbol defined by a member of a library that is a
364relocatable object file.
365This speeds up linking to the library, and allows routines in the library
366to call each other regardless of their placement within the library.
367.Sh SEE ALSO
368.Xr ar 1 ,
369.Xr cc 1 ,
370.Xr ld 1 ,
371.Xr nm 1 ,
372.Xr intro 2 ,
373.Xr math 3 ,
374.Xr stdio 3 ,
375.Xr make.conf 5 ,
376.Xr src.conf 5
377.Sh HISTORY
378An
379.Nm
380manual appeared in
381.At v7 .
382