What is a Library Function?
A library function is one of the many functions that constitute the standard C library. The purposes of these functions are very diverse, covering tasks such as opening a file, converting a time value into a human-readable format, comparing two character strings, allocating memory, and performing formatted output.
Unlike system calls, which always cause a transition into kernel mode, library functions may or may not use system calls internally. Some library functions work entirely in user space, while others are layered on top of system calls and provide a more programmer-friendly interface.
Key Concepts
Library Functions vs System Calls
- Part of the standard C library
- May or may not use system calls
- Run entirely in user space
- Provide a higher-level, friendlier interface
- Can provide buffering, formatting, error handling
- Examples:
printf(),fopen(),malloc()
- Direct kernel service requests
- Always cause a mode switch to kernel mode
- Fixed set with unique numbers
- Low-level and architecture-specific
- Examples:
open(),write(),brk()
Many library functions do not make any system calls at all. String manipulation functions fall into this category. For example, strlen(), strcmp(), strcat(), and memcpy() all perform their work entirely within user space without ever touching the kernel.
/* These work purely in user space — no system calls */
size_t len = strlen("hello"); /* No kernel involvement */
int cmp = strcmp("abc", "abd"); /* Purely user-space computation */
Other library functions are implemented by making one or more system calls. The library function fopen(), for instance, internally uses the open() system call to open a file. Similarly, printf() eventually calls the write() system call to write formatted output to the file descriptor.
/* fopen() is a library function layered on the open() system call */
FILE *fp = fopen("data.txt", "r");
/* Internally calls: open("data.txt", O_RDONLY) system call */
/* printf() is a library function layered on the write() system call */
printf("Hello, world!\n");
/* Internally buffers output, then calls: write(1, buf, n) */
Library functions often provide a more caller-friendly interface than the underlying system call. A key example is memory allocation. The malloc() and free() library functions perform a range of bookkeeping tasks that make them a much easier way to allocate and free memory than the underlying brk() system call.
/* printf() provides formatting + buffering — write() has neither */
printf("Value: %d\n", 42); /* Library function: formatted + buffered */
write(1, "Value: 42\n", 10); /* System call: raw bytes, no formatting */
/* malloc() provides bookkeeping — brk() requires manual management */
void *ptr = malloc(1024); /* Library function: tracked allocation */
/* brk() would require calculating new program break address manually */
The GNU C Library (glibc)
There are different implementations of the standard C library on various UNIX systems. The most commonly used implementation on Linux is the GNU C Library (glibc). It is available at http://www.gnu.org/software/libc/.
The principal developer and maintainer of glibc was initially Roland McGrath. This task was subsequently carried out by Ulrich Drepper. Most applications developed on Linux use glibc as their C library implementation.
| C Library | Use Case | URL |
|---|---|---|
| glibc | Standard Linux systems, desktop, server | gnu.org/software/libc/ |
| uClibc | Embedded devices with smaller memory | uclibc.org |
| diet libc | Minimal footprint for embedded applications | fefe.de/dietlibc/ |
There are several ways to determine the version of glibc installed on a system. Each method is useful in different scenarios:
The glibc shared library file can be executed directly from the shell. When run as an executable, it displays version information and other details.
$ /lib/libc.so.6
GNU C Library stable release version 2.10.1, by Roland McGrath et al.
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
Compiled by GNU CC version 4.4.0 20090506 (Red Hat 4.4.0-4).
Compiled on a Linux >>2.6.18-128.4.1.el5<< system on 2009-08-19.
Available extensions:
The C stubs add-on version 2.1.2.
crypt add-on version 2.1 by Michael Glad and others
GNU Libidn by Simon Josefsson
Native POSIX Threads Library by Ulrich Drepper et al
BIND-8.2.3-T5B
RT using linux kernel aio
Some Linux distributions place glibc at a pathname other than /lib/libc.so.6. Use the ldd (list dynamic dependencies) command against any dynamically linked executable to find the library’s actual location.
$ ldd myprog | grep libc
libc.so.6 => /lib/tls/libc.so.6 (0x4004b000)
From glibc version 2.0 onward, two constants are defined for compile-time version testing. On a system with glibc 2.12, these constants have the values 2 and 12 respectively.
#include <stdio.h>
int main(void) {
#ifdef __GLIBC__
printf("glibc major version: %d\n", __GLIBC__);
printf("glibc minor version: %d\n", __GLIBC_MINOR__);
#else
printf("Not using glibc\n");
#endif
return 0;
}
/* Limitation: These constants reflect the compile-time glibc version,
not the run-time version if the program runs on a different system */
To determine the version of glibc available at run time (rather than compile time), call gnu_get_libc_version(). This function returns a pointer to a version string such as "2.12".
#include <gnu/libc-version.h>
const char *gnu_get_libc_version(void);
/* Returns pointer to null-terminated, statically allocated string
containing the GNU C library version number */
/* Usage example */
#include <stdio.h>
#include <gnu/libc-version.h>
int main(void) {
printf("glibc runtime version: %s\n", gnu_get_libc_version());
return 0;
}
Version information can also be obtained using the confstr() function with the glibc-specific configuration variable _CS_GNU_LIBC_VERSION. This call returns a string such as "glibc 2.12".
#include <unistd.h>
#include <stdio.h>
int main(void) {
char buf[100];
confstr(_CS_GNU_LIBC_VERSION, buf, sizeof(buf));
printf("glibc version string: %s\n", buf);
/* Output example: glibc 2.12 */
return 0;
}
| Method | Compile-Time? | Run-Time? | Best Used When |
|---|---|---|---|
Run libc.so.6 directly |
N/A (shell only) | Yes | Quick manual inspection |
ldd | grep libc |
N/A (shell only) | Yes | Finding library path |
__GLIBC__ / __GLIBC_MINOR__ |
Yes | No | Conditional compilation with #ifdef |
gnu_get_libc_version() |
No | Yes | Programs that may run on other systems |
confstr(_CS_GNU_LIBC_VERSION) |
No | Yes | When a full version string including “glibc” prefix is needed |
- A library function is one of many functions in the standard C library, serving diverse purposes from string manipulation to file I/O.
- Some library functions make no system calls; others are layered on top of system calls; many provide richer, more convenient interfaces than raw system calls.
- The primary C library on Linux is glibc (GNU C Library), used by most Linux applications.
- Alternatives like uClibc and diet libc exist for embedded or memory-constrained environments.
- glibc version can be detected at compile time via
__GLIBC__/__GLIBC_MINOR__or at runtime viagnu_get_libc_version(). - For programs that run on different systems, always prefer runtime version detection over compile-time constants.
Next: Error Handling in System Programming
Learn how to check and handle return values from system calls and library functions.
