Find glibc version in your system

In this guide at OpenGenus, we present 3 different ways to identify compile time and run time version of glibc. The version of glibc has two components: Major version and Minor version. It is as follows:

glibc version

glibc MAJOR_VERSION.MINOR_VERSION
glibc 2.31

MAJOR_VERSION = 2
MINOR_VERSION = 31

In short, you can find out the glibc version using the following command:

ldd --version

We find the glibc version using the following ways:

  • Method 1: Use ldd
  • Method 2: Use libc-version.h in C++ code
  • Method 3: gnu_get_libc_version()

glibc is GNU C Library which provides the Standard C implementation and also, supports C++. It provides the core libraries in Linux kernel and hence, is used in every operating system that uses Linux as the kernel like RHEL, CentOS, Ubuntu and many others.

For a certain software, the glibc version should be the same in the destination system as in the origin system where the software has been compiled. You may have faced glibc errors and hence, need to find out the glibc version you are using.

Following are the methods:

Method 1: Use ldd

Use the following command in the terminal:

ldd --version

The output will give the version of glibc used:

ldd (Ubuntu GLIBC 2.31-0ubuntu9.2) 2.31
Copyright (C) 2020 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.
Written by Roland McGrath and Ulrich Drepper.

Method 2: Use libc-version.h in C++ code

Add the following C++ code in a file named "glibc.cpp":

// Part of iq.OpenGenus.org
#include <stdio.h>
#include <stdlib.h>
#include <gnu/libc-version.h>

int main(int argc, char *argv[])
{
    // Print glibc version
    printf("GNU libc version: %s\n", gnu_get_libc_version());
    exit(EXIT_SUCCESS);
}

Compile the above code using the following command:

g++ -std=c++11 glibc.c -o glibc

Execute the above code:

./glibc

The output will be:

GNU libc version: 2.31

Method 3: gnu_get_libc_version()

In this method, we can identify two versions of glibc:

  • Compile time version of glibc
  • Runtime version of glibc

The compile time version of glibc is defined in macros __GLIBC__ and __GLIBC_MINOR__.

The run time version of glibc is available using the function gnu_get_libc_version().

Note that the run time version of glibc can be greater than the compile time version of glibc but never smaller. The major version is likely to be the same.

Add the following code in a file named "glibc.c":

// Part of iq.OpenGenus.org
#include <stdio.h>
#ifdef __GLIBC__
#include <gnu/libc-version.h>
#endif

int
main(void)
{
    #ifdef __GLIBC__
        printf("GNU libc compile-time version: %u.%u\n", 
                __GLIBC__, __GLIBC_MINOR__);
        printf("GNU libc runtime version:      %s\n", 
                gnu_get_libc_version());
        return 0;
    #else
        puts("Not the GNU C Library");
        return 1;
    #endif
}

Compile the above code using the following command:

g++ -std=c++11 glibc.c -o glibc

Execute the above code:

./glibc

The output is as follows:

GNU libc compile-time version: 2.31
GNU libc runtime version:      2.31

With this, you know how to find out the version of glibc. Happy debugging.