繁体   English   中英

C:函数'getpwnam_r'的隐式声明

[英]C: implicit declaration of function ‘getpwnam_r’

在检索密码数据库中记录的断开字段(例如,本地密码文件/etc/passwd ,NIS和LDAP)时,我使用的是getpwnam_rhttp:// 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.

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