Get Linux Kernel Version using C++

Get Linux Kernel Version using C++

Linux kernel is the main component of the Linux operating system. It gets regular updates and releases with new features and bug fixes. Retrieving the Linux kernel version using C++ can be useful for various purposes in Linux development, such as system monitoring, troubleshooting issues, debugging, or application compatibility. This tutorial explains how to get the Linux kernel version using C++.

The uname function can be used to get information about the current Linux kernel. In the following example, the uname function is called with a struct utsname object. The uname function fills the osInfo structure with information about the Linux system, including the kernel version. It can be accessed using the osInfo.release field. The extracted kernel version is then printed to the console.

#include <iostream>
#include <sys/utsname.h>

int main()
{
    struct utsname osInfo{};
    uname(&osInfo);
    std::cout << osInfo.release << std::endl;

    return 0;
}

The output might look like this:

5.19.0-38-generic

Leave a Comment

Cancel reply

Your email address will not be published.