简体   繁体   中英

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.

man getuid :

SYNOPSIS

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

You're missing those includes.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM