在Linux环境下编一个工程,无论怎么编译总是出现错误,提示如下:
bmeta_info.cpp:4280: undefined reference to `crypt’
collect2: ld returned 1 exit status
网上查的资料表示:
Undefined reference to 错误:这类错误是在连接过程中出现的,可能有两种原因∶一是使用者自己定义的函数或者全局变量所在源代码文件,没有被编译、连接,或者干脆还没有定义,这 需要使用者根据实际情况修改源程序,给出全局变量或者函数的定义体;二是未定义的符号是一个标准的库函数,在源程序中使用了该库函数,而连接过程中还没有 给定相应的函数库的名称,或者是该档案库的目录名称有问题
使用 man crypt命令后从中找到如下信息:
GNU EXTENSION
The glibc2 version of this function has the following additional features. If salt is a character string starting with the three characters "$1$" followed by at most eight characters, and optionally terminated by "$", then instead of using the DES machine, the glibc crypt function uses an MD5-based algorithm, and outputs up to 34 bytes, namely "$1$<string>$", where "<string>" stands for the up to 8 characters following "$1$" in the salt, followed by 22 bytes chosen from the set [a–zA–Z0–9./]. The entire key is significant here (instead of only the first 8 bytes).
Programs using this function must be linked with -lcrypt.
最重要的是最后一句”Programs using this function must be linked with -lcrypt.”,即链接需要加入-lctypt
解决方法:g++ -lcrypt XXXX