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 27, 2024 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/libm.a 272the math library 273.El 274.Sh LIBRARY TYPES 275The system libraries are located in 276.Pa /lib 277and 278.Pa /usr/lib . 279A library has the following naming convention: 280.Bd -unfilled -offset indent 281libc.so.7 282.Ed 283.Pp 284Libraries with an 285.Sq .a 286suffix are static. 287When a program is linked against a static library, all necessary library code 288will be included in the binary. 289This means the binary can be run even when the libraries are unavailable. 290However, it can be inefficient with both disk space and memory usage 291during execution. 292The C compiler, 293.Xr cc 1 , 294can be instructed to link statically by specifying the 295.Fl static 296flag. 297.Pp 298Libraries with a 299.Sq .so.X 300suffix are dynamic libraries. 301When code is linked dynamically, the library code that the application needs 302is not included in the binary. 303Instead, data structures are added containing information about which dynamic 304libraries to link with. 305When the binary is executed, the run-time linker 306.Xr ld.so 1 307reads these data structures and loads them into the 308process virtual address space. 309.Xr rtld 1 310loads the shared libraries when the program is executed. 311.Pp 312.Sq X 313represents the library version number of the library. 314In the example above, a binary linked with 315.Pa libc.so.8 316would not be usable on a system where only 317.Pa libc.so.7 318is available. 319.Pp 320The advantages of dynamic libraries are that multiple instances of the same 321library can share address space, and the physical size of the binary is 322smaller. 323A namespace per shared library is available via hidden visibility, 324allowing multiple compilation units in a library to share things without 325making them available to other libraries. 326It is possible to load libraries dynamically via 327.Xr dlopen 3 . 328The disadvantage is the added complexity that comes with loading the 329libraries dynamically, and the extra time taken to load the libraries. 330Of course, if the libraries are not available, the binary will be unable 331to execute. 332Calls across shared libraries are also slightly slower and cannot be 333inlined, not even with link time optimization. 334The C compiler, 335.Xr cc 1 , 336can be instructed to link dynamically by specifying the 337.Fl shared 338flag. 339.Pp 340Shared libraries, as well as static libraries on architectures which produce 341position-independent executables 342.Pq PIEs 343by default, contain position-independent code 344.Pq PIC . 345Normally, compilers produce relocatable code. 346Relocatable code needs to be modified at run-time, depending on where in 347memory it is to be run. 348The C compiler, 349.Xr cc 1 , 350can be instructed to generate PIC code by specifying the 351.Fl fPIC 352flag. 353.Pp 354Static libraries are generated using the 355.Xr ar 1 356utility. 357The libraries contain an index to the contents of the library, 358stored within the library itself. 359The index lists each symbol defined by a member of a library that is a 360relocatable object file. 361This speeds up linking to the library, and allows routines in the library 362to call each other regardless of their placement within the library. 363.Sh SEE ALSO 364.Xr ar 1 , 365.Xr cc 1 , 366.Xr ld 1 , 367.Xr nm 1 , 368.Xr intro 2 , 369.Xr math 3 , 370.Xr stdio 3 , 371.Xr make.conf 5 , 372.Xr src.conf 5 373.Sh HISTORY 374An 375.Nm 376manual appeared in 377.At v7 . 378