简体   繁体   English

crypt函数和链接错误“未定义引用'crypt'”

[英]crypt function and link error “undefined reference to 'crypt'”

I have used the crypt function in c to encrypt the given string. 我在c中使用了crypt函数来加密给定的字符串。 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'". 但上面的代码引发了一个错误,“未定义引用`crypt'”。 What is the problem in the above code. 上面的代码有什么问题。

Thanks in advance. 提前致谢。

If you want to use the crypt() function, you need to link to the library that supplies it. 如果要使用crypt()函数,则需要链接到提供它的库。 Add -lcrypt to your compile command. -lcrypt添加到编译命令中。

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> . 较早版本的glibc为此提供了一个libcrypt库,并在<unistd.h>声明了该函数 - 为了针对此支持进行编译,您可能还需要在包含<unistd.h>之前在代码中定义_XOPEN_SOURCE_GNU_SOURCE

Newer versions of glibc don't supply libcrypt - it is instead provided by a separate libxcrypt . 较新版本的glibc不提供libcrypt - 而是由单独的libxcrypt You still link with -lcrypt , but the function is instead declared in <crypt.h> . 您仍然与-lcrypt链接,但该函数在<crypt.h>声明。

crypt() uses DES which is extremely insecure and probably older than you 12 years older than you. crypt()使用的DES 非常不安全可能比你年长12岁。

I suggest you use a serious encryption algorithm, such as AES. 我建议你使用严格的加密算法,比如AES。 Many libraries offer such encryption; 许多图书馆提供此类加密; OpenSSL (crypto.lib) is a good choice for example. 例如,OpenSSL(crypto.lib)是一个不错的选择。

Not answering your actual question since a lot of people already did 因为很多人已经做过,所以没有回答你的实际问题

You need to include crypt.h if you want to use crypt() . 如果要使用crypt()则需要包含crypt.h。 Below your other two includes, add: 在您的其他两个包括下方,添加:

#include <crypt.h>

您需要在包含之前添加以下行:

#define _XOPEN_SOURCE

#include头文件之前,你必须#define __XOPEN_SOURCE

The crypt function is non-standard, but is supplied as an extension by the GNU C library on Linux. crypt函数是非标准的,但是由Linux上的GNU C库作为扩展提供。 It's defined in <crypt.h> 它在<crypt.h>定义

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM