对“crypt'”的未定义引用;

对“crypt'”的未定义引用;,c,cryptography,linker-errors,crypt,C,Cryptography,Linker Errors,Crypt,我正在使用我在网络中找到的以下代码,当我试图构建它时,我遇到了一个错误。编译还可以 以下是错误: /tmp/ccCnp11F.o: In function `main': crypt.c:(.text+0xf1): undefined reference to `crypt' collect2: ld returned 1 exit status 代码如下: #include <stdio.h> #include <time.h> #include <un

我正在使用我在网络中找到的以下代码,当我试图构建它时,我遇到了一个错误。编译还可以

以下是错误:

/tmp/ccCnp11F.o: In function `main':

crypt.c:(.text+0xf1): undefined reference to `crypt'

collect2: ld returned 1 exit status
代码如下:

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

 int main()
 {
   unsigned long seed[2];
   char salt[] = "$1$........";
   const char *const seedchars =
     "./0123456789ABCDEFGHIJKLMNOPQRST"
     "UVWXYZabcdefghijklmnopqrstuvwxyz";
   char *password;
   int i;

   /* Generate a (not very) random seed.
      You should do it better than this... */
   seed[0] = time(NULL);
   seed[1] = getpid() ^ (seed[0] >> 14 & 0x30000);

   /* Turn it into printable characters from `seedchars'. */
   for (i = 0; i < 8; i++)
     salt[3+i] = seedchars[(seed[i/5] >> (i%5)*6) & 0x3f];

   /* Read in the user's password and encrypt it. */
   password = crypt(getpass("Password:"), salt);

   /* Print the results. */
   puts(password);
   return 0;
 }
#包括
#包括
#包括
#包括
int main()
{
无符号长种子[2];
焦盐[]=“$1$……”;
常量字符*常量种子字符=
“/0123456789ABCDEFGHIJKLMNOPQRST”
“uvwxyzabcdefghijklmnopqrstuvxyz”;
字符*密码;
int i;
/*生成一个(不太)随机种子。
你应该做得比这更好*/
种子[0]=时间(空);
种子[1]=getpid()^(种子[0]>>14&0x30000);
/*将其从“seedchars”转换为可打印字符*/
对于(i=0;i<8;i++)
盐[3+i]=种子字符[(种子[i/5]>>(i%5)*6)&0x3f];
/*读入用户的密码并加密*/
password=crypt(getpass(“password:”),salt);
/*打印结果*/
输入(密码);
返回0;
}

您可能忘记链接库

  gcc ..... -lcrypt
crypt.c:(.text+0xf1):对“crypt”的未定义引用是链接器错误


尝试链接到
-lcrypt
gcc crypt.c-lcrypt
编译时必须添加-lcrypt。。。假设源文件名为crypttest.c,您将执行以下操作:

cc -lcrypt -o crypttest crypttest.c

这可能是由于两个原因:

  • 与crypt库链接:使用
    -l
    作为
    gcc
    的标志
    示例:
    gcc-lcrypt
    其中
    crypt.h
    已编译到库中
  • 文件
    crypt.h
    不在
    包含路径中。只有当头文件位于
    包含路径中时,才能在头文件周围使用
    标记。要确保include路径中存在
    crypt.h
    ,请使用
    -I
    标志,如下所示:
    gcc-我…

    示例:
    gcc-I./crypt
    其中当前目录的
    crypt/sub目录中存在
    crypt.h

    如果您不想使用
    -I
    标志,请将
    #include
    更改为
    #include“crypt.h”

    的可能重复项没有编译错误,只有链接错误。预处理器指令是不相关的。