【问题标题】:utsname/uname in C++ [closed]C++ 中的 utsname/uname [关闭]
【发布时间】:2021-05-15 12:57:33
【问题描述】:

我正在编写一个程序,它使用 标头和名称函数来显示操作系统名称、版本等。我已经包含了标头并调用了该函数,但是,我得到了指出无法识别头文件的致命错误。我在网上看到的所有内容都显示了我用作代码示例的 main.cpp 文件。正确链接此头文件的任何帮助都会有很大帮助!

我目前在 VS、CLion 和 csegrid 上运行。

【问题讨论】:

    标签: c++ linux-kernel uname


    【解决方案1】:

    我收到致命错误,指出无法识别头文件

    您需要先安装提供该头文件(以及更多)的软件包。

    Ubuntu:

    sudo apt install linux-libc-dev
    

    Fedora:

    sudo dnf install glibc-headers
    

    如果您使用任何其他操作系统,则需要使用操作系统提供的工具找到正确的软件包

    然后,如果你有其他一切,这应该编译并显示信息:

    #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
    }
    

    我的 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
    

    【讨论】:

      猜你喜欢
      • 2015-03-26
      • 2017-07-24
      • 1970-01-01
      • 2011-04-05
      • 2022-01-23
      • 1970-01-01
      • 2017-12-20
      • 2022-07-23
      • 1970-01-01
      相关资源
      最近更新 更多