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
undefined reference to `crypt'
提问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:
这可能是由于两个原因:
- Linking with the crypt library: use
-l<nameOfCryptLib>as a flag togcc.
Example:gcc ... -lcryptwherecrypt.hhas been compiled into a library. - The file
crypt.his not in theinclude path. You can use<and>tags around a header file only when the file is in theinclude path. To ensure thatcrypt.his present in the include path, use the-Iflag, like so:gcc ... -I<path to directory containing crypt.h> ...
Example:gcc -I./cryptwherecrypt.his present in thecrypt/ sub-directoryof the current directory.
- 与 crypt 库链接:
-l<nameOfCryptLib>用作gcc.
示例:gcc ... -lcryptwherecrypt.h已编译成库。 - 该文件
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"

