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

发布于 2024-11-09 16:41:15 字数 383 浏览 7 评论 0原文

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

我正在使用 crypt() 函数来加密密码。

我以前从未使用过该函数,因此我查看了主页:

man 3 crypt

它说要包含 unistd.h 标头。

然而,当我这样做时,我收到了关于 crypt 函数的隐式警告。

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

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

I'm using GCC 4.6.0 (on an otherwise unidentified platform).

I am using the crypt() function to encrypt a password.

I have never used that function before so I checked out the main page:

man 3 crypt

And it says to include the unistd.h header.

However, when I did that, I got an implicit warning for the 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. However, how come it doesn't say that in the man page?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

路还长,别太狂 2024-11-16 16:41:15

我的手册页中还显示了 #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));

It also says #define _XOPEN_SOURCE (before including unistd.h) in my man page. So you should probably add it to expose the declaration of crypt.

EDIT

I just tried it. Including unistd.h and #define _XOPEN_SOURCE before it does the trick. 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:

/* 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));
静若繁花 2024-11-16 16:41:15

crypt() 的 POSIX 标准表示它应该在 中声明,所以这就是您需要包含的内容。

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

我目前使用一个名为 "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 标准和扩展。

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

The POSIX standard for crypt() says that it should be declared in <unistd.h>, so that's what you need to include.

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:

#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. 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.

And this works when I set GCC into -std=c99 mode. If you use -std=gnu99, you probably don't need the header at all; it automatically enables C99 standard plus extensions.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文