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

问题描述 投票:0回答:2
#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());
                        ^

我只是想不出在这个范围内没有声明getuid的原因是什么。我想我已经包含了所有必要的头文件。(yami对R的回答getlogin() c function returns NULL and error "No such file or directory"的评论我试过在网上搜索但是找不到任何相关的答案。

c++ getpwuid
2个回答
6
投票

man getuid

SYNOPSIS

#include <unistd.h>
#include <sys/types.h>

uid_t getuid(void);
uid_t geteuid(void);

你错过了那些包括。


© www.soinside.com 2019 - 2024. All rights reserved.