The Standard C Library; The GNU C Library (glibc)

There are different implementations of the standard C library on the various UNIX implementations. The most commonly used implementation on Linux is the GNU C library (glibc, http://www.gnu.org/software/libc/).

Note

The principal developer and maintainer of the GNU C library was initially Roland McGrath. Nowadays, this task is carried out by Ulrich Drepper.

Various other C libraries are available for Linux, including libraries with smaller memory requirements for use in embedded device applications. Examples include uClibc (http://www.uclibc.org/) and diet libc (http://www.fefe.de/dietlibc/). In this book, we confine the discussion to glibc, since that is the C library used by most applications developed on Linux.

Sometimes, we need to determine the version of glibc on a system. From the shell, we can do this by running the glibc shared library file as though it were an executable program. When we run the library as an executable, it displays various text, including its version number:

$ /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
For bug reporting instructions, please see:
<http://www.gnu.org/software/libc/bugs.html>.

In some Linux distributions, the GNU C library resides at a pathname other than /lib/libc.so.6. One way of determining the location of the library is to run the ldd (list dynamic dependencies) program against an executable linked dynamically against glibc (most executables are linked in this manner). We can then inspect the resulting library dependency list to find the location of the glibc shared library:

$ ldd myprog | grep libc
        libc.so.6 => /lib/tls/libc.so.6 (0x4004b000)

There are two means by which an application program can determine the version of the GNU C library present on the system: by testing constants or by calling a library function. From version 2.0 onward, glibc defines two constants, __GLIBC__ and __GLIBC_MINOR__, that can be tested at compile time (in #ifdef statements). On a system with glibc 2.12 installed, these constants would have the values 2 and 12. However, these constants are of limited use in a program that is compiled on one system but run on another system with a different glibc. To handle this possibility, a program can call the gnu_get_libc_version() function to determine the version of glibc available at run time.

#include <gnu/libc-version.h>

const char *gnu_get_libc_version(void);

Note

Returns pointer to null-terminated, statically allocated string containing GNU C library version number

The gnu_get_libc_version() function returns a pointer to a string, such as 2.12.