Get CPU Architecture on Linux using C++

Get CPU Architecture on Linux using C++

One of the essential information that developer may require to get is the CPU architecture of the Linux system. The CPU architecture defines the basic instruction set, such as x86, x86_64, etc. This tutorial shows how to get CPU architecture on Linux using C++.

The uname function can be used to get information about the current Linux kernel. In the following example, we call the uname function by passing utsname structure. Then it is filled with information about the Linux system. The machine field of the utsname structure contains the hardware name of the current system, which indicates the CPU architecture.

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

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

    return 0;
}

Here's an example output of the provided code snippet:

x86_64

Leave a Comment

Cancel reply

Your email address will not be published.