对“crypt”的未定义引用
我正在使用下面的代码,我在网络的某个地方找到了,当我尝试构建它时,我收到了一个错误。 编译没问题。
这是错误:
/tmp/ccCnp11F.o: In function `main': crypt.c:(.text+0xf1): undefined reference to `crypt' collect2: ld returned 1 exit status
这是代码:
#include #include #include #include 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 > (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'
是链接器错误。
尝试与-lcrypt
链接: gcc crypt.c -lcrypt
。
编译时你要添加-lcrypt …想象一下源文件名为crypttest.c,你会做:
cc -lcrypt -o crypttest crypttest.c
你有可能忘记链接图书馆
gcc ..... -lcrypt
这可能是由于两个原因:
- 链接到crypt库:使用
-l
作为gcc
的标志。
示例:gcc ... -lcrypt
其中crypt.h
已编译到库中。 - 文件
crypt.h
不在include path
。 仅当文件位于include path
时,才能在头文件周围使用<
和>
标记。 要确保包含路径中存在crypt.h
,请使用-I
标志,如下所示:gcc ... -I
...
示例:gcc -I./crypt
其中crypt.h
存在于当前目录的crypt/ sub-directory
。
如果您不想使用-I
标志,请将#include
更改为#include "crypt.h"