5

我正在使用 GCC 4.6.0(在其他无法识别的平台上)。

我正在使用该crypt()功能来加密密码。

我以前从未使用过该功能,所以我查看了主页:

man 3 crypt

它说要包括unistd.h标题。

但是,当我这样做时,我得到了该crypt函数的隐含警告。

warning: implicit declaration of function ‘crypt’ [-Wimplicit-function-declaration]

我做了一些搜索,发现您必须包含crypt.h. 但是,为什么手册页中没有这样说?

4

2 回答 2

3

它还在我的手册页中说#define _XOPEN_SOURCE(包括之前)。unistd.h因此,您可能应该添加它以公开crypt.

编辑

我刚试过。包括unistd.h #define _XOPEN_SOURCE之前的伎俩。仅包括它是不够的。

使用

gcc version 4.6.0 20110429
GNU C Library stable release version 2.13

调查unistd.h

/* XPG4.2 specifies that prototypes for the encryption functions must
   be defined here.  */
#ifdef  __USE_XOPEN
/* Encrypt at most 8 characters from KEY using salt to perturb DES.  */
extern char *crypt (__const char *__key, __const char *__salt)
     __THROW __nonnull ((1, 2));
于 2011-05-25T16:45:20.920 回答
3

POSIX 标准crypt()说它应该在 中声明<unistd.h>,所以这就是您需要包含的内容。

但是,根据您指定的其他编译器选项,您可能会或可能不会看到它。

我目前使用我调用"posixver.h"的包含代码的标题:

#ifndef JLSS_ID_POSIXVER_H
#define JLSS_ID_POSIXVER_H

/*
** Include this file before including system headers.  By default, with
** C99 support from the compiler, it requests POSIX 2001 support.  With
** C89 support only, it requests POSIX 1997 support.  Override the
** default behaviour by setting either _XOPEN_SOURCE or _POSIX_C_SOURCE.
*/

/* _XOPEN_SOURCE 700 is loosely equivalent to _POSIX_C_SOURCE 200809L */
/* _XOPEN_SOURCE 600 is loosely equivalent to _POSIX_C_SOURCE 200112L */
/* _XOPEN_SOURCE 500 is loosely equivalent to _POSIX_C_SOURCE 199506L */

#if !defined(_XOPEN_SOURCE) && !defined(_POSIX_C_SOURCE)
#if __STDC_VERSION__ >= 199901L
#define _XOPEN_SOURCE 600   /* SUS v3, POSIX 1003.1 2004 (POSIX 2001 + Corrigenda) */
#else
#define _XOPEN_SOURCE 500   /* SUS v2, POSIX 1003.1 1997 */
#endif /* __STDC_VERSION__ */
#endif /* !_XOPEN_SOURCE && !_POSIX_C_SOURCE */

#endif /* JLSS_ID_POSIXVER_H */

在我工作的系统上,设置_XOPEN_SOURCE为 700 将是一种沮丧和徒劳的练习,无论我多么希望能够这样做。但这些选项通常使我的代码在 Linux、HP-UX、MacOS X、AIX 和 Solaris 上正常工作——我通常在这些类 Unix 平台上工作。

这在我将 GCC 设置为-std=c99模式时有效。如果您使用-std=gnu99,您可能根本不需要标题;它会自动启用 C99 标准加扩展。

顺便说一句,我曾经在单个源文件的顶部有这节。随着包含该节的文件数量增加(侵占数百个文件),我意识到当我需要调整设置时,我面临着一项巨大的编辑工作。现在我有了一个标头,并且我正在将它改装到具有该节的文件中,因此我更改了一个文件(标头)以对我的所有代码进行更改 - 一旦我完成了我所做的破坏。

于 2011-05-26T05:20:53.777 回答