9

我正在使用我在网络某处找到的以下代码,当我尝试构建它时出现错误。编译没问题。

这是错误:

/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;
 }
4

4 回答 4

22

crypt.c:(.text+0xf1): undefined reference to 'crypt'是链接器错误。

尝试与-lcrypt:链接gcc crypt.c -lcrypt

于 2011-05-13T08:51:46.883 回答
2

您必须在编译时添加 -lcrypt ...想象一下源文件名为 crypttest.c,您将执行以下操作:

cc -lcrypt -o crypttest crypttest.c
于 2011-05-13T08:52:21.043 回答
0

您可能忘记链接库

  gcc ..... -lcrypt
于 2011-05-13T08:50:56.637 回答
-1

这可能是由于两个原因:

  1. 与 crypt 库链接:-l<nameOfCryptLib>用作gcc.
    示例:gcc ... -lcryptwherecrypt.h已编译成库。
  2. 该文件crypt.h不在include path. 只有<>文件位于include path. 要确保它crypt.h存在于包含路径中,请使用-I标志,如下所示:gcc ... -I<path to directory containing crypt.h> ...
    示例:当前目录 gcc -I./cryptcrypt.h存在的位置。crypt/ sub-directory

如果您不想使用该-I标志,请将其更改#include<crypt.h>#include "crypt.h"

于 2011-05-13T09:21:34.127 回答