Get glibc Version using C++

Get glibc Version using C++

The GNU C Library also known as glibc, is an open-source implementation of the C standard library. It provides essential C functions for Linux and other Unix-based operating systems. Sometimes, it may be necessary to retrieve the version of glibc programmatically for troubleshooting, compatibility checks, or other purposes. This tutorial shows how to get the glibc version using C++.

We provide two methods which can be used to obtain the glibc version in a C++ program. The retrieved version is printed to the console. The output might look like this:

2.35

1. GLIBC macros

The __GLIBC__ and __GLIBC_MINOR__ macros can be used to retrieve the major and minor version numbers of the glibc library, respectively. These macros allow determining the precise version of glibc installed on the system.

#include <iostream>

int main()
{
    std::cout << __GLIBC__ << "." << __GLIBC_MINOR__ << std::endl;

    return 0;
}

2. gnu_get_libc_version function

The gnu_get_libc_version function from the gnu/libc-version.h header can be used to obtain the version of glibc as a string. If the function returns nullptr, it indicates that the glibc version could not be retrieved.

#include <iostream>
#include <gnu/libc-version.h>

int main()
{
    std::cout << gnu_get_libc_version() << std::endl;

    return 0;
}

Leave a Comment

Cancel reply

Your email address will not be published.