Неопределенная ссылка на `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 <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 ответа

Решение

crypt.c:(.text+0xf1): undefined reference to 'crypt' ошибка компоновщика

Попробуйте связаться с -lcrypt: gcc crypt.c -lcrypt,

Вы должны добавить -lcrypt при компиляции... Представьте, что исходный файл называется crypttest.c, вы сделаете:

cc -lcrypt -o crypttest crypttest.c

Скорее всего, вы забыли связать библиотеку

  gcc ..... -lcrypt

This could be due to two reasons:

  1. Linking with the crypt library: use -l<nameOfCryptLib> as a flag to gcc,
    Пример: gcc ... -lcrypt где crypt.h has been compiled into a library.
  2. Файл crypt.h не в include path, Ты можешь использовать < а также > tags around a header file only when the file is in the include path, Чтобы убедиться, что crypt.h is present in the include path, use the -I flag, like so: gcc ... -I<path to directory containing crypt.h> ...
    Пример: gcc -I./crypt где crypt.h присутствует в crypt/ sub-directory of the current directory.

If you do not want to use the -I flag, change the #include<crypt.h> в #include "crypt.h"

Другие вопросы по тегам