提示collect2: ld returned 1 exit status

本文详细记录了在使用Qt进行项目编译过程中遇到的各种错误及其解决方法,包括库文件缺失、链接器错误等问题,并提供了具体的步骤指导。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在QT官网下的qtsdk.bin

然后安装后使用,无法编译。就连它自带的示例也是同样的错误提示

这是个在QtCreator下的提示:

:-1: error: collect2: ld returned 1 exit status

好心给帮看看,谢谢了

解决办法:

不使用QtCreator生成应用程序,

在终端方方式下,转到工作目录下

1 qmake -project

2 qmake

此时生成一个Makefile,修改这个文件,将“LIBS          =”行中“-pthread“后至末尾删除

3 make

好了,成功。

不知道有没有办法,让qmake不生成含那些字串的Makeifle.

还有什么疑问

 

应该不是qmake的错误,大概是楼主本人的系统原有的一些lib与要编译安装的有冲突。

 

可能是pro文件出错了,我也经常遇到这种错误,如果是QT-=gui/ core就改成QT +=gui/core或者直接把这行去掉,可能吧,反正是该pro文件的问题

 

 

 和老婆合作一个毕业设计嵌入式开发与设计,她做QT部分,我做内核裁减部分。今天闲来无聊,想学学QT。于是打开了QT Assistant,里面内容很丰富的说,从QT历史到QT使用,一应具全(哎,相当初要知道这个,就不会为了安装QT费那么大的劲了)。看看里面的example吧。好,先来个helloworld吧,按照文档一步一步的做:
    1、源码
      
#include <QApplication>
#include <QPushButton>
int main(int argc, char *argv[])
{
     QApplication app(argc, argv);
     QPushButton hello("Hello world!");
     hello.resize(100, 30);
     hello.show();
     return app.exec();
 } 
 

       保存成main.cpp

     2、打开cmd,进入main.cpp的目录,执行以下命令

        qmake -project  // 为了生成project文件

        qmake           //生成Makefile文件

        make            //生成可执行文件

        出问题了,

      

make -f Makefile.Debug
make[1]: Entering directory `C:/Qt/mypro'
g++ -c -g -g -frtti -fexceptions -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL
-DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"C:/Qt/4.1.2/
include/QtCore" -I"C:/Qt/4.1.2/include/QtGui" -I"C:/Qt/4.1.2/include" -I"." -I"C
:/Qt/4.1.2/include/ActiveQt" -I"debug" -I"." -I"C:/Qt/4.1.2/mkspecs/win32-g++" -
o debug/main.o main.cpp
main.cpp:10:4: warning: no newline at end of file
g++ -mthreads -Wl,-enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runt
ime-pseudo-reloc -Wl,-subsystem,windows -o "debug/mypro.exe" debug/main.o -L"C:
/Qt/4.1.2/lib" -lmingw32 -lqtmaind -lQtGuid4 -lQtCored4
c:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../mingw32/bin/ld.exe: cannot f
ind -lQtGuid4
collect2: ld returned 1 exit status
make[1]: *** [debug/mypro.exe] Error 1
make[1]: Leaving directory `C:/Qt/mypro'
make: *** [debug] Error 2

       提示mingw的ld.exe找不到QTGuid4库文件,我到QT安

        装目录的lib里查看了一下,确实没有此库,只有libQTGui4.a这

        个库文件,这个。。。。当机立断,把libQTGui4.a复制了一个,

        更改名字为libQTGuid4.a,在进行make ,不错,那个错误提示没有

        了,但是有提示找不到QTCored4库文件,

       

make -f Makefile.Debug
make[1]: Entering directory `C:/Qt/mypro'
g++ -c -g -g -frtti -fexceptions -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL
-DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"C:/Qt/4.1.2/
include/QtCore" -I"C:/Qt/4.1.2/include/QtGui" -I"C:/Qt/4.1.2/include" -I"." -I"C
:/Qt/4.1.2/include/ActiveQt" -I"debug" -I"." -I"C:/Qt/4.1.2/mkspecs/win32-g++" -
o debug/main.o main.cpp
main.cpp:10:4: warning: no newline at end of file
g++ -mthreads -Wl,-enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runt
ime-pseudo-reloc -Wl,-subsystem,windows -o "debug/mypro.exe" debug/main.o -L"C:
/Qt/4.1.2/lib" -lmingw32 -lqtmaind -lQtGuid4 -lQtCored4
c:/Dev-Cpp/bin/../lib/gcc/mingw32/3.4.2/../../../../mingw32/bin/ld.exe: cannot f
ind -lQtGuid4
collect2: ld returned 1 exit status
make[1]: *** [debug/mypro.exe] Error 1
make[1]: Leaving directory `C:/Qt/mypro'
make: *** [debug] Error 2

        照葫芦画瓢,再复制一份

        libQTCored4.a,在运行make ,顺利通过。

        虽然错误解决了,但是不知道是为什么,按说应该在安装QT时产生

        那两个库文件的,可是。。。。反正是奇怪,继续寻找答案

PS:注意这个警告

main.cpp:10:4: warning: no newline at end of file

错误原由: Each instance of a backslash character (/) immediately followed by a
new-line character is deleted, splicing physical source lines to form logical
source lines. Only the last backslash on any physical source line shall be
eligible for being part of such a splice. A source file that is not empty
shall end in a new-line character, which shall not be immediately preceded
by a backslash character before any such splicing takes place.

改正方法:在main函数结束后打个回车符号

 

chmod u+x qt-sdk-linux-x86-opensource-2009.02.bin
./qt-sdk-linux-x86-opensource-2009.02.bin
接着出现安装,,,,,

安装结束,,,,,
运行如下代码时
#include <QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QLabel *label = new QLabel("Hello Qt!");
label->show();
return app.exec();
}

Qt Creator  编译运行,,,
Starting: /usr/bin/make -w
make: Entering directory `/home/sun/Desktop/Myprojects/hello'
g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I../../../qt/qt/mkspecs/linux-g++ -I. -I../../../qt/qt/include/QtCore -I../../../qt/qt/include/QtGui -I../../../qt/qt/include -I. -I. -I. -o hello.o hello.cpp
g++ -Wl,-O1 -Wl,-rpath,/home/sun/qt/qt/lib -o hello hello.o -L/home/sun/qt/qt/lib -lQtGui -L/home/sun/qt/qt/lib -L/usr/X11R6/lib -pthread -lfreetype -lgobject-2.0 -lSM -lICE -pthread -pthread -lXrender -lfontconfig -lXext -lX11 -lQtCore -lm -pthread -lgthread-2.0 -lrt -lglib-2.0 -ldl -lpthread
/usr/bin/ld: cannot find -lfreetype
collect2: ld returned 1 exit status
make: *** [hello] Error 1
make: Leaving directory `/home/sun/Desktop/Myprojects/hello'
Exited with code 2.
Error while building project hello
When executing build step 'Make'

解决  /usr/bin/ld: cannot find -lfreetype

安装  libfreetype-dev

(用synaptic Package Manager 选 libfreetype6 和libfreetype6-dev )

编译时出现:
/usr/bin/ld: cannot find -lgobject-2.0
collect2: ld returned 1 exit status
make: *** [hello] Error 1
make: Leaving directory `/home/sun/Desktop/pro/hello'
Exited with code 2.
Error while building project hello
When executing build step 'Make'

安装:
gob2                2.0.15-1
libsm6
libsm-dev
libxrender-dev
fontconfig-dev
libxetx-dev

此时即可解决,,,,,,
编译、运行正常,,,

 


 

### 三级标题:理解 'collect2: error: ld returned 1 exit status' 错误 `collect2: error: ld returned 1 exit status` 是 GCC 编译器在链接阶段遇到错误时返回的错误信息。该错误通常表明链接器(ld)在尝试生成可执行文件或共享库时未能成功完成任务。具体而言,错误信息中的 `ld returned 1 exit status` 表示链接器返回了非零退出状态,通常是由于链接失败所致[^2]。 ### 三级标题:常见原因及解决方法 1. **缺少必要的库文件** 当链接器无法找到指定的库文件时,会报出类似 `/usr/bin/ld: cannot find -lxxx` 的错误。例如,在引用中提到的 `/usr/bin/ld: cannot find -lfreebl3` 和 `/usr/bin/ld: cannot find -lstdc++` 均属于此类问题[^1]。 解决方法包括: - 确认所需库是否已正确安装。例如,`libstdc++` 是 C++ 标准库的一部分,通常可以通过安装 GCC 工具链来获取。 - 检查链接器搜索路径是否包含所需库的目录。可以通过 `-L` 选项指定库路径,如 `-L/usr/local/lib`。 - 确保在链接命令中正确指定了库名,例如 `-lstdc++` 应出现在链接命令的最后部分。 2. **库路径配置错误** 在引用中提到的 `EXTRA_SHARED_LIBS += -L$(DIST)/lib -L$(NSSUTIL_LIB_DIR) -lnssutil3 -lfreebl3 ...` 表明链接时需要指定多个库路径和库文件。如果路径配置不正确,链接器将无法找到所需的库文件[^1]。 解决方法包括: - 确认 `$(DIST)/lib`、`$(NSSUTIL_LIB_DIR)` 和 `$(NSPR_LIB_DIR)` 等变量是否指向正确的库目录。 - 使用 `ls` 或 `find` 命令验证库文件是否存在,例如 `ls /usr/lib/libfreebl3.so`。 3. **依赖库版本不兼容** 有时,即使库文件存在,也可能因为版本不匹配导致链接失败。例如,项目可能需要 `libstdc++` 的某个特定版本,而系统中安装的是另一个版本。 解决方法包括: - 使用 `ldconfig -p | grep libstdc++` 查看系统中可用的库版本。 - 如果需要特定版本,可以通过软链接(symbolic link)将所需的库版本链接到标准路径中,例如 `ln -s /usr/local/lib/libstdc++.so.6 /usr/lib/libstdc++.so`。 4. **链接顺序问题** GCC 链接器在处理静态库时,会按照命令行中的顺序解析符号。如果某个库在使用它的目标文件之前被链接,可能会导致未解析的符号错误。 解决方法包括: - 确保库文件出现在链接命令的最后部分,例如 `gcc main.o -o program -lmylib`。 - 如果多个库之间存在依赖关系,应按照依赖顺序排列库文件,例如 `gcc main.o -o program -ldependent -lmainlib`。 5. **环境变量配置问题** 有时,系统的环境变量(如 `LD_LIBRARY_PATH`)可能未正确配置,导致运行时无法找到所需的共享库。 解决方法包括: - 使用 `export LD_LIBRARY_PATH=/path/to/lib:$LD_LIBRARY_PATH` 临时添加库路径。 - 将库路径添加到 `/etc/ld.so.conf` 或用户级配置文件中,并运行 `ldconfig` 更新缓存。 ### 三级标题:示例代码:修复链接错误 以下是一个简单的示例,展示了如何修复由于缺少库文件导致的链接错误: ```bash # 假设编译时出现以下错误: # /usr/bin/ld: cannot find -lstdc++ # collect2: error: ld returned 1 exit status # 解决方法: # 1. 安装 libstdc++ 开发包 sudo apt-get install libstdc++-dev # 2. 确认库文件是否存在 ls /usr/lib/x86_64-linux-gnu/libstdc++.so # 3. 重新编译并链接 g++ main.cpp -o program -lstdc++ ``` ### 三级标题:总结 `collect2: error: ld returned 1 exit status` 是一个常见的链接错误,通常由库文件缺失、路径配置错误或依赖关系未正确解析引起。通过检查库文件是否存在、确认链接路径是否正确以及调整链接顺序,可以有效解决此类问题[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值