I am currently working on pset2 crack, and I have a question regarding the use of crypt(). This is the synopsis I have got from man:

#define _XOPEN_SOURCE       /* See feature_test_macros(7) */
#include <unistd.h>

char *crypt(const char *key, const char *salt);

#define _GNU_SOURCE         /* See feature_test_macros(7) */
#include <crypt.h>

char *crypt_r(const char *key, const char *salt,
              struct crypt_data *data);

Link with -lcrypt.

This synopsis implies to me that to use the crypt() function, you have to define _XOPEN_SOURCE and include unistd.h. Likewise, to use the crypt_r() function, you have to define _GNU_SOURCE and include crypt.h. However, it appears that the only way I can get crypt() to work is to include crypt.h. No definitions necessary. Why is this? And how would I know this from the man page?

1 Answer 1


It will work when constructed as in the man page. From man feature_test_macros:

NOTE: In order to be effective, a feature test macro must be defined before including any header files. This can be done either in the compilation command (cc -DMACRO=value) or by defining the macro within the source code before including any headers.

The definition(s) have to be before any includes, perhaps that's why it wasn't working for you.

You can't know from the man page that you "don't need" the define/include, nor even that crypt.h exists.

This comment /* XPG4.2 specifies that prototypes for the encryption functions must be defined here. */ from /usr/include/unistd.h sheds some light. The man page matches the spec. It has to do with POSIX conformance and possibly US export restrictions of encryption software. I suspect that crypt.h is a "historical artifact".

  • Thanks very much, putting the definition at the top worked. I was sure that I had already tried this though, I tested it fairly exhaustively beforehand. I must have just been wrong! Jan 21, 2017 at 18:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .