[英]C: implicit declaration of function ‘getpwnam_r’
While retrieving the broken-out fields of the record in the password database (eg, the local password file /etc/passwd
, NIS, and LDAP) that matches a provided username name, I am using the getpwnam_r
( http://linux.die.net/man/3/getpwnam_r ) function. 在检索密码数据库中记录的断开字段(例如,本地密码文件
/etc/passwd
,NIS和LDAP)时,我使用的是getpwnam_r
( http:// linux。 die.net/man/3/getpwnam_r )功能。
#define __USE_BSD
#define _BSD_SOURCE
#include <pwd.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
int
main(int argc, char *argv[])
{
struct passwd pwd;
struct passwd *result;
char *buf;
size_t bufsize;
int s;
if (argc != 2) {
fprintf(stderr, "Usage: %s username\n", argv[0]);
exit(EXIT_FAILURE);
}
bufsize = sysconf(_SC_GETPW_R_SIZE_MAX);
if (bufsize == -1) /* Value was indeterminate */
bufsize = 16384; /* Should be more than enough */
buf = malloc(bufsize);
if (buf == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
s = getpwnam_r(argv[1], &pwd, buf, bufsize, &result);
if (result == NULL) {
if (s == 0)
printf("Not found\n");
else {
errno = s;
perror("getpwnam_r");
}
exit(EXIT_FAILURE);
}
printf("Name: %s; UID: %ld\n", pwd.pw_gecos, (long) pwd.pw_uid);
exit(EXIT_SUCCESS);
}
The code works fine, but Eclipse shows me a warning as follows: 代码工作正常,但Eclipse向我显示如下警告:
warning: implicit declaration of function ‘getpwnam_r’ [-Wimplicit-function-declaration]
How could I fix it? 我该怎么办呢?
Note that I'm currently using Ubuntu 14.04 LTS . 请注意,我目前正在使用Ubuntu 14.04 LTS 。
To use this function you need two includes : 要使用此功能,您需要两个包括:
#include <sys/types.h>
#include <pwd.h>
Add them and it should not complain anymore. 添加它们,它不应该再抱怨了。
You can see that by running man getpwnam_r
. 你可以通过运行
man getpwnam_r
来看到它。
You also need to define either __USE_MISC
or __USE_SVID
since it a POSIX only function. 您还需要定义
__USE_MISC
或__USE_SVID
因为它只是POSIX函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.