crypt function and link error “undefined reference to 'crypt'”

流过昼夜 提交于 2019-11-27 07:50:17

问题


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.


回答1:


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>.




回答2:


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




回答3:


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

#include <crypt.h>



回答4:


You need to put the following line before your includes:

#define _XOPEN_SOURCE



回答5:


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




回答6:


The crypt function is non-standard, but is supplied as an extension by the GNU C library on Linux. It's defined in <crypt.h>



来源:https://stackoverflow.com/questions/2565427/crypt-function-and-link-error-undefined-reference-to-crypt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!