代码之家  ›  专栏  ›  技术社区  ›  Shobhit

为什么我得到“getuid未在该范围内声明”错误?

  •  0
  • Shobhit  · 技术社区  · 6 年前
    #include <string>
    #include <stdio.h>
    #include <pwd.h>
    
    std::string impPath()
    {
        char *name;
        struct passwd *pass;
        pass = getpwuid(getuid());
        name = pass->pw_name;
    
        std::string PATH = "/home";
        PATH.append("/");
        PATH.append(name);
    
        return PATH;
    }
    

    我需要知道 用户名 用户的。为了做到这一点。我正在使用 getpwuid() 但我得到了这个错误。

    /home/shobhit/Desktop/file.cpp:15: error: 'getuid' was not declared in this scope
     pass = getpwuid(getuid());
                            ^
    

    我就是不明白为什么 获取用户ID 未在此范围内声明。我想我已经包含了所有必要的头文件。(Yami对R的回答有评论) getlogin() c function returns NULL and error "No such file or directory" 我试过在网上搜索,但找不到任何相关的答案。

    2 回复  |  直到 6 年前
        1
  •  6
  •   melpomene    6 年前

    man getuid 以下内容:

    简介

    #include <unistd.h>
    #include <sys/types.h>
    
    uid_t getuid(void);
    uid_t geteuid(void);
    

    你错过了包括在内的东西。

        2
  •  1
  •   yakobyd    6 年前