Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

crypt function and link error "undefined reference to 'crypt'"

I have used the crypt function in c to encrypt the given string. I have written the following code,

#include<stdio.h>
#include<unistd.h>

int main()
{
    printf("%s\n",crypt("passwd",1000));
}

But the above code threw an error ,"undefined reference to `crypt'". What is the problem in the above code.

Thanks in advance.

like image 824
kiruthika Avatar asked Apr 02 '10 06:04

kiruthika


4 Answers

If you want to use the crypt() function, you need to link to the library that supplies it. Add -lcrypt to your compile command.

Older versions of glibc supplied a libcrypt library for this purpose, and declared the function in <unistd.h> - to compile against this support, you may also need to define either _XOPEN_SOURCE or _GNU_SOURCE in your code before including <unistd.h>.

Newer versions of glibc don't supply libcrypt - it is instead provided by a separate libxcrypt. You still link with -lcrypt, but the function is instead declared in <crypt.h>.

like image 162
caf Avatar answered Sep 20 '22 11:09

caf


crypt() uses DES which is extremely insecure and probably older than you 12 years older than you.

I suggest you use a serious encryption algorithm, such as AES. Many libraries offer such encryption; OpenSSL (crypto.lib) is a good choice for example.

Not answering your actual question since a lot of people already did

like image 27
Thomas Bonini Avatar answered Sep 18 '22 11:09

Thomas Bonini


You have to #define __XOPEN_SOURCE before you #include the header files.

like image 39
Qwerty Avatar answered Sep 17 '22 11:09

Qwerty


You need to include crypt.h if you want to use crypt(). Below your other two includes, add:

#include <crypt.h>
like image 33
Chad Birch Avatar answered Sep 19 '22 11:09

Chad Birch