简体   繁体   English

对“crypt”的未定义引用

[英]undefined reference to `crypt'

I am using the below code that i found somewhere in the net and i am getting an error when i try to build it. 我正在使用下面的代码,我在网络的某个地方找到了,当我尝试构建它时,我收到了一个错误。 The compilation is ok. 编译还可以。

Here is the error: 这是错误:

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

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

collect2: ld returned 1 exit status

and here is the code: 这是代码:

#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;
 }

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

Try linking with -lcrypt : gcc crypt.c -lcrypt . 尝试使用-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 . 链接到crypt库:使用-l<nameOfCryptLib>作为gcc的标志。
    Example: gcc ... -lcrypt where crypt.h has been compiled into a library. 示例: gcc ... -lcrypt其中crypt.h已编译到库中。
  2. The file crypt.h is not in the include path . 文件crypt.h不在include path You can use < and > tags around a header file only when the file is in the include path . 仅当文件位于include path时,才可以在头文件周围使用<>标记。 To ensure that crypt.h is present in the include path, use the -I flag, like so: gcc ... -I<path to directory containing crypt.h> ... 要确保包含路径中存在crypt.h ,请使用-I标志,如下所示: gcc ... -I<path to directory containing crypt.h> ...
    Example: gcc -I./crypt where crypt.h is present in the crypt/ sub-directory of the current directory. 示例: gcc -I./crypt其中crypt.h存在于当前目录的crypt/ sub-directory

If you do not want to use the -I flag, change the #include<crypt.h> to #include "crypt.h" 如果您不想使用-I标志,请将#include<crypt.h>更改为#include "crypt.h"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM