[英]C: implicit declaration of function ‘getpwnam_r’
在檢索密碼數據庫中記錄的斷開字段(例如,本地密碼文件/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);
}
代碼工作正常,但Eclipse向我顯示如下警告:
warning: implicit declaration of function ‘getpwnam_r’ [-Wimplicit-function-declaration]
我該怎么辦呢?
請注意,我目前正在使用Ubuntu 14.04 LTS 。
要使用此功能,您需要兩個包括:
#include <sys/types.h>
#include <pwd.h>
添加它們,它不應該再抱怨了。
你可以通過運行man getpwnam_r
來看到它。
您還需要定義__USE_MISC
或__USE_SVID
因為它只是POSIX函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.