crypt() 函数是在 unistd.h 还是 crypt.h 中声明的?
我正在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的手册页中还显示了
#define _XOPEN_SOURCE
(在包含unistd.h
之前)。因此,您可能应该添加它来公开 crypt 的声明。编辑
我刚刚尝试过。在执行此操作之前包含
unistd.h
和#define _XOPEN_SOURCE
。仅包括它是不够的。使用
查看
unistd.h
:It also says
#define _XOPEN_SOURCE
(before includingunistd.h
) in my man page. So you should probably add it to expose the declaration ofcrypt
.EDIT
I just tried it. Including
unistd.h
and#define _XOPEN_SOURCE
before it does the trick. Including it alone isn't enough.Using
Looking into
unistd.h
:crypt()
的 POSIX 标准表示它应该在
中声明,所以这就是您需要包含的内容。但是,根据您指定的其他编译器选项,您可能会看到它,也可能看不到它。
我目前使用一个名为
"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: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.