Bryce Porter
Bryce Porter

Reputation: 23

utsname/uname in C++

I am writing a program that uses the <sys/utsname.h> header and the name functions to display the operating systems name, version, etc. I have included the header and have called the function, however, I am getting fatal errors stating that the header file is not recognized. Everything I have seen online shows the main.cpp file which I have used as an example for my code. Any help to get this header file properly linked would be of much help!

I am currently running on VS, CLion, and on csegrid.

Upvotes: 1

Views: 2062

Answers (1)

Ted Lyngmo
Ted Lyngmo

Reputation: 117832

I am getting fatal errors stating that the header file is not recognized

You need to install the package which provides that header file (and much more) first.

Ubuntu:

sudo apt install linux-libc-dev

Fedora:

sudo dnf install glibc-headers

If you use any other OS, you need to find the correct package using the tools provided with the OS

Then, if you have everything else in place, this should compile and display the information:

#include <sys/utsname.h>

#include <iostream>

// a small helper to display the content of an utsname struct:
std::ostream& operator<<(std::ostream& os, const utsname& u) {
    return os << "sysname : " << u.sysname << '\n'
              << "nodename: " << u.nodename << '\n'
              << "release : " << u.release << '\n'
              << "version : " << u.version << '\n'
              << "machine : " << u.machine << '\n';
}

int main() {
    utsname result;      // declare the variable to hold the result

    uname(&result);      // call the uname() function to fill the struct

    std::cout << result; // show the result using the helper function
}

Example output from my Ubuntu 20.04 (WSL2):

sysname : Linux
nodename: TED-W10
release : 4.19.104-microsoft-standard
version : #1 SMP Wed Feb 19 06:37:35 UTC 2020
machine : x86_64

Upvotes: 2

Related Questions