Неопределенная ссылка на `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:
- Linking with the crypt library: use
-l<nameOfCryptLib>
as a flag togcc
,
Пример:gcc ... -lcrypt
гдеcrypt.h
has been compiled into a library. - Файл
crypt.h
не вinclude path
, Ты можешь использовать<
а также>
tags around a header file only when the file is in theinclude 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"