为什么我会收到"getuid was not declared in that scope"错误?

Why I am getting "getuid was not declared in that scope" error?

本文关键字:in declared that scope 错误 not was getuid 为什么      更新时间:2023-10-16
#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函数返回NULL和错误"没有这样的文件或目录" 我尝试在网上搜索,但找不到任何相关的答案。

man getuid

概要

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

您缺少这些包含。

getuid()

pwd.h中不存在。见 http://pubs.opengroup.org/onlinepubs/7908799/xsh/pwd.h.html