c++getpwuid

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


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


Solution

  • man getuid:

    SYNOPSIS

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

    You're missing those includes.