2011-05-13 219 views
5

我正在使用下面的代码,我发现网络中的某个地方,并且当我尝试构建它时出现错误。汇编是好的。未定义的引用'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 = 
    "./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

可能重复://计算器。 com/questions/2565427/crypt-function-and-link-error-undefined-reference-to-crypt) – jww 2016-11-23 01:50:55

回答

12

crypt.c:(.text+0xf1): undefined reference to 'crypt'是一个连接错误。

尝试链接-lcryptgcc crypt.c -lcrypt

+0

没有编译错误,只链接错误。预处理器指令是不相关的。 – 2016-11-23 07:48:46

0

机会是你忘了链接库

gcc ..... -lcrypt 
1

您对编译时添加-lcrypt ......试想一下,源文件被称为crypttest.c,你会做:

cc -lcrypt -o crypttest crypttest.c 
+1

很多编译器上的链接标志需要结束 – sehe 2011-05-13 08:57:58

+0

不知道,谢谢 – roirodriguez 2011-05-13 09:01:35

0

这可能是由于两方面的原因:

  1. 链接到crypt库:使用-l<nameOfCryptLib>作为标记gcc
    示例:gcc ... -lcrypt其中crypt.h已被编译为库。
  2. 档案crypt.h不在include path中。只有当文件位于include path时,才可以在头文件周围使用<>标签。要确保crypt.h存在于包含路径中,请使用-I标志,如下所示:gcc ... -I<path to directory containing crypt.h> ...
    示例:gcc -I./crypt其中crypt.h存在于当前目录的crypt/ sub-directory中。

如果你不想使用-I标志,改变[隐窝功能和链接错误“未定义的引用‘地穴’”(HTTP的#include<crypt.h>#include "crypt.h"