C语言 对“crypt”的未定义引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5989444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:38:42  来源:igfitidea点击:

undefined reference to `crypt'

ccryptographylinker-errorscrypt

提问by Stelios

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[] = "$........";
   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;
 }

回答by Michael Foukarakis

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

回答by rtheitroadodriguez

You've to add -lcrypt when compiling... Imagine the source file is called crypttest.c, you'll do:

您必须在编译时添加 -lcrypt... 假设源文件名为 crypttest.c,您将执行以下操作:

cc -lcrypt -o crypttest crypttest.c

回答by sehe

Chances are you forget to link the library

您可能忘记链接库

  gcc ..... -lcrypt

回答by Sriram

This could be due to two reasons:

这可能是由于两个原因:

  1. Linking with the crypt library: use -l<nameOfCryptLib>as a flag to gcc.
    Example: gcc ... -lcryptwhere crypt.hhas been compiled into a library.
  2. The file crypt.his not in the include path. You can use <and >tags around a header file only when the file is in the include path. To ensure that crypt.his present in the include path, use the -Iflag, like so: gcc ... -I<path to directory containing crypt.h> ...
    Example: gcc -I./cryptwhere crypt.his present in the crypt/ sub-directoryof the current directory.
  1. 与 crypt 库链接:-l<nameOfCryptLib>用作gcc.
    示例:gcc ... -lcryptwherecrypt.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当前目录。

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

如果您不想使用该-I标志,请将#include<crypt.h>其更改为#include "crypt.h"