Library Functions & GNU C Library

 

Library Functions & GNU C Library
Understanding glibc, Library Functions vs System Calls, and Version Detection
3.2–3.3
Chapter Sections
glibc
Primary C Library
2+
Alternatives Available

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 Function GNU C Library (glibc) fopen() printf() malloc() brk() system call libc.so.6 uClibc diet libc gnu_get_libc_version() confstr() ldd command

Library Functions vs System Calls

Library Functions
  • 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()
System Calls
  • 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()

Three Types of Library Functions by System Call Usage
No System Calls Layered on System Calls Extended Wrappers
Type 1: No System Calls Used

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 */
Type 2: Layered on Top of System Calls

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) */
Type 3: Friendlier Interface with Added Features

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)

About glibc
Primary Linux C Library Roland McGrath Ulrich Drepper

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/

Determining the glibc Version

There are several ways to determine the version of glibc installed on a system. Each method is useful in different scenarios:

Method 1: Run the Shared Library as an Executable

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
Method 2: Using ldd to Find the Library Path

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)
Method 3: Compile-Time Constants (__GLIBC__ and __GLIBC_MINOR__)

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 */
Limitation: Compile-time constants are of limited use for programs compiled on one system but run on another with a different version of glibc. Use the runtime function below for portable version checks.
Method 4: Runtime Version via gnu_get_libc_version()

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;
}
Method 5: Using confstr() with _CS_GNU_LIBC_VERSION

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;
}

Version Detection Methods – Comparison
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

Summary of Key Points
Library Functions glibc System Call Layering Version Detection
  • 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 via gnu_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.

Error Handling → ← System Calls

Leave a Reply

Your email address will not be published. Required fields are marked *