Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
879 views
in Technique[技术] by (71.8m points)

c - Is the crypt() function declared in unistd.h or 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...