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

ant*_*009 5 c crypt

我正在使用GCC 4.6.0(在另一个未经识别的平台上).

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

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

man 3 crypt
Run Code Online (Sandbox Code Playgroud)

它说包括unistd.h标题.

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

warning: implicit declaration of function ‘crypt’ [-Wimplicit-function-declaration]
Run Code Online (Sandbox Code Playgroud)

我做了一些搜索,我发现你必须包括crypt.h.但是,为什么在man手册页中没有说呢?

cni*_*tar 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
Run Code Online (Sandbox Code Playgroud)

调查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));
Run Code Online (Sandbox Code Playgroud)

  • @ant2009:行 `#define _XOPEN_SOURCE 700` 需要位于所有包含文件之前 - 如果您将其放在其他一些包含文件之后,即使它位于 `<unistd.h>` 之前,它也不起作用。 (5认同)