简体   繁体   English

crypt() function 是在 unistd.h 还是 crypt.h 中声明的?

[英]Is the crypt() function declared in unistd.h or crypt.h?

I'm using GCC 4.6.0 ( on an otherwise unidentified platform ).我正在使用 GCC 4.6.0(在其他无法识别的平台上)。

I am using the crypt() function to encrypt a password.我正在使用crypt() function 来加密密码。

I have never used that function before so I checked out the main page:我之前从未使用过 function 所以我查看了主页:

man 3 crypt

And it says to include the unistd.h header.它说包括unistd.h header。

However, when I did that, I got an implicit warning for the crypt function.但是,当我这样做时,我收到了一个隐含的crypt function 警告。

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

I did a bit of searching and I found that you have to include the crypt.h .我做了一些搜索,发现您必须包含crypt.h However, how come it doesn't say that in the man page?但是,为什么手册页中没有这样说?

It also says #define _XOPEN_SOURCE (before including unistd.h ) in my man page.它还在我的手册页中说#define _XOPEN_SOURCE (包括unistd.h之前)。 So you should probably add it to expose the declaration of crypt .因此,您可能应该添加它以公开crypt的声明。

EDIT编辑

I just tried it.我刚试过。 Including unistd.h and #define _XOPEN_SOURCE before it does the trick.包括unistd.h#define _XOPEN_SOURCE在它起作用之前。 Including it alone isn't enough.仅包括它是不够的。

Using使用

gcc version 4.6.0 20110429
GNU C Library stable release version 2.13

Looking into unistd.h : 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));

The POSIX standard for crypt() says that it should be declared in <unistd.h> , so that's what you need to include. crypt()的 POSIX 标准说它应该在<unistd.h>中声明,所以这就是你需要包含的内容。

However, depending on what other compiler options you specify, you may or may not see it.但是,根据您指定的其他编译器选项,您可能会或可能不会看到它。

I currently use a header I call "posixver.h" which contains the code:我目前使用 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 */

On the systems where I work, setting _XOPEN_SOURCE to 700 would be an exercise in frustration and futility, however much I'd like to be able to do so.在我工作的系统上,将_XOPEN_SOURCE设置为 700 将是一种挫败感和徒劳的练习,无论我多么希望能够这样做。 But these options normally make my code work correctly on Linux, HP-UX, MacOS X, AIX and Solaris - the Unix-like platforms I normally work on.但是这些选项通常使我的代码在 Linux、HP-UX、MacOS X、AIX 和 Solaris 上正常工作——我通常在这些类 Unix 平台上工作。

And this works when I set GCC into -std=c99 mode.这在我将 GCC 设置为-std=c99模式时有效。 If you use -std=gnu99 , you probably don't need the header at all;如果您使用-std=gnu99 ,您可能根本不需要 header; it automatically enables C99 standard plus extensions.它会自动启用 C99 标准加扩展。

Incidentally, I used to have this stanza at the top of individual source files.顺便说一句,我曾经在单个源文件的顶部有这节。 As the number of files containing the stanza grew (encroaching on hundreds of files), I realized that when I needed to adjust the settings, I had a monstrous editing job ahead of me.随着包含该节的文件数量增加(侵占数百个文件),我意识到当我需要调整设置时,我面临着一项巨大的编辑工作。 Now I have the one header and I'm retrofitting it into the files that have the stanza so I change one file (the header) to effect a change for all my code - once I've finished undoing the damage I did.现在我有一个 header 并且我正在将其改装到具有该节的文件中,因此我更改了一个文件(标题)以对我的所有代码进行更改 - 一旦我完成了我所做的破坏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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