Linux gcc编译error:"collect2: ld returned 1 exit status"

在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

【参考:http://blog.sina.com.cn/s/blog_64c238860100wzoo.html

Linux编译报错"undefined reference to `main'"通常是由于缺少main函数引起的。main函数是C/C++程序的入口函数,编译器在链接阶段会查找main函数作为程序的起点。如果找不到main函数,就会报错"undefined reference to `main'"。 可能的原因和解决方法如下: 1. 检查源代码文件是否包含了正确的main函数,并确保函数签名正确。C程序的main函数应该是`int main(void)`或者`int main(int argc, char *argv[])`,C++程序的main函数应该是`int main()`或者`int main(int argc, char *argv[])`。 2. 确保源代码文件的文件名后缀符合编译器的要求。C程序的源代码文件通常使用`.c`后缀,C++程序的源代码文件通常使用`.cpp`或`.cc`后缀。 3. 确保编译命令正确。在使用gcc或g++编译器时,需要将源代码文件作为参数传递给编译器,并使用`-o`选项指定输出文件名。例如,对于C程序,可以使用以下命令进行编译:`gcc -o output_file source_file.c`;对于C++程序,可以使用以下命令进行编译:`g++ -o output_file source_file.cpp`。 4. 如果使用了多个源代码文件,确保所有的源代码文件都被包含在编译命令中。例如,对于C程序,可以使用以下命令进行编译:`gcc -o output_file source_file1.c source_file2.c`;对于C++程序,可以使用以下命令进行编译:`g++ -o output_file source_file1.cpp source_file2.cpp`。 如果以上方法都没有解决问题,可能是编译器或链接器的配置有误,可以尝试重新安装编译器或检查相关配置。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值