2011-05-13 24 views
5

Estoy usando el siguiente código que encontré en algún lugar de la red y recibo un error cuando intento compilarlo. La compilación está bien.referencia indefinida a 'crypt'

Aquí está el error:

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

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

collect2: ld returned 1 exit status 

y aquí está el código:

#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 = 
    "./ABCDEFGHIJKLMNOPQRST" 
    "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; 
} 
+0

duplicado posible de [cripta y la función de enlace de error "referencia indefinida a 'cripta'"] (http: // stackoverflow. com/questions/2565427/crypt-function-and-link-error-undefined-reference-to-crypt) – jww

Respuesta

12

crypt.c:(.text+0xf1): undefined reference to 'crypt' es un error de vinculador.

Intente vincular con -lcrypt: gcc crypt.c -lcrypt.

+0

No hay errores de compilación, solo errores de enlace. Las directivas de preprocesador son irrelevantes. –

0

probable es que se olvide de vincular la biblioteca

gcc ..... -lcrypt 
1

súmale -lcrypt al compilar ... Imagínese el archivo de origen se llama crypttest.c, que va a hacer:

cc -lcrypt -o crypttest crypttest.c 
+1

en muchos compiladores, la bandera de enlace debe estar al final – sehe

+0

No lo sabía, gracias – roirodriguez

0

Esto podría ser debido a dos razones:

  1. Enlazando con la biblioteca crypt: use -l<nameOfCryptLib> como una bandera para gcc.
    Ejemplo: gcc ... -lcrypt donde crypt.h se ha compilado en una biblioteca.
  2. El archivo crypt.h no está en el include path. Puede usar las etiquetas < y > alrededor de un archivo de encabezado solo cuando el archivo se encuentre en el include path. Para asegurarse de que crypt.h está presente en la ruta de inclusión, usar la bandera -I, así: gcc ... -I<path to directory containing crypt.h> ...
    Ejemplo: gcc -I./crypt donde crypt.h está presente en la crypt/ sub-directory del directorio actual.

Si no desea utilizar la bandera -I, cambie el #include<crypt.h> a #include "crypt.h"