Search code examples
cmultithreadinggnucrypt

Using crypt_r on OS X


I want to use the crypt_r function on Mac OS X 10.8.2

#define _GNU_SOURCE
#include <crypt.h>

produces

crypt.h: No such file or directory

Where can I get the crypt.h file from? Or am I including it wrong?

Edited question - concrete example

#include <unistd.h>
#include <stdlib.h>

int main(){
    struct crypt_data * data = (struct crypt_data *) malloc(sizeof(struct crypt_data));
    char * testhash;
    testhash = crypt_r("string", "sa", data);
    free(data);
    return 0;
}

produces

gcc test.c -Wall
test.c: In function ‘main’:
test.c:5: error: invalid application of ‘sizeof’ to incomplete type ‘struct crypt_data’ 
test.c:7: warning: implicit declaration of function ‘crypt_r’
test.c:7: warning: assignment makes pointer from integer without a cast

Solution

  • Edit: crypt_r() is not available on OS X.

    Original answer:

    The contents of <crypt.h> on OS X is handled by <unistd.h>. So, instead of

    #define _GNU_SOURCE
    #include <crypt.h>
    

    simply write

    #include <unistd.h>
    

    in order to access the crypt() function.