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

2.9k views Asked by At
#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;
}

I need to know username of the user. In order to do this. I am using getpwuid() but I am getting this error.

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

I just couldn't figure out what is the reason that getuid is not declared in this scope. I think I have included all the necessary header files.(yami's comment on R's answer getlogin() c function returns NULL and error "No such file or directory" I have tried searching on the web but couldn't find any relevant answer.

2

There are 2 answers

5
melpomene On BEST ANSWER

man getuid:

SYNOPSIS

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

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

You're missing those includes.

0
yakobyd On

getuid() is not present in pwd.h. See http://pubs.opengroup.org/onlinepubs/7908799/xsh/pwd.h.html