[英]Is the crypt() function declared in unistd.h or crypt.h?
我正在使用 GCC 4.6.0(在其他无法识别的平台上)。
我正在使用crypt()
function 来加密密码。
我之前从未使用过 function 所以我查看了主页:
man 3 crypt
它说包括unistd.h
header。
但是,当我这样做时,我收到了一个隐含的crypt
function 警告。
warning: implicit declaration of function ‘crypt’ [-Wimplicit-function-declaration]
我做了一些搜索,发现您必须包含crypt.h
。 但是,为什么手册页中没有这样说?
它还在我的手册页中说#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));
crypt()
的 POSIX 标准说它应该在<unistd.h>
中声明,所以这就是你需要包含的内容。
但是,根据您指定的其他编译器选项,您可能会或可能不会看到它。
我目前使用 header 我称之为"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
,您可能根本不需要 header; 它会自动启用 C99 标准加扩展。
顺便说一句,我曾经在单个源文件的顶部有这节。 随着包含该节的文件数量增加(侵占数百个文件),我意识到当我需要调整设置时,我面临着一项巨大的编辑工作。 现在我有一个 header 并且我正在将其改装到具有该节的文件中,因此我更改了一个文件(标题)以对我的所有代码进行更改 - 一旦我完成了我所做的破坏。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.