DEV Community

BC
BC

Posted on

How to get glibc version - C Lang

Here we list 3 methods to get glibc version:

Method 1, ldd

The easiest way is to use ldd command which comes with glibc and in most cases it will print the same version as glibc:

$ ldd --version
ldd (Ubuntu GLIBC 2.30-0ubuntu2.1) 2.30
Enter fullscreen mode Exit fullscreen mode

Here "2.30" is the version.

Method2, libc.so

We can also use the libc.so to print out version. First we need to find out the path of libc.so. You can use ldd to a binary to find out the libc.so:

$ ldd `which ls` | grep libc
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f918034d000)
Enter fullscreen mode Exit fullscreen mode

From where we can see the libc.so path is /lib/x86_64-linux-gnu/libc.so.6.

Then simply run:

$ /lib/x86_64-linux-gnu/libc.so.6
GNU C Library (Ubuntu GLIBC 2.30-0ubuntu2.1) stable release version 2.30.
Enter fullscreen mode Exit fullscreen mode

WE get the versin "2.30".

Method 3, programmatic way

We can also write a program to print the version out. There are 3 ways we can do this programmatically.

#include <gnu/libc-version.h>
#include <stdio.h>
#include <unistd.h>

int main() {
    // method 1, use macro
    printf("%d.%d\n", __GLIBC__, __GLIBC_MINOR__);

    // method 2, use gnu_get_libc_version 
    puts(gnu_get_libc_version());

    // method 3, use confstr function
    char version[30] = {0};
    confstr(_CS_GNU_LIBC_VERSION, version, 30);
    puts(version);

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Compile and run:

$ gcc main.c -o main
$ ./main
2.30
2.30
glibc 2.30
Enter fullscreen mode Exit fullscreen mode

Top comments (0)