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.
#include <math.h>
and-lm
, for an analogy. – Stuffy-std=c99
one of the options, for example? – Ponderous