c++ - 编译Qt hello world程序时出现Undefined reference错误

标签 c++ qt

我正在尝试编译一个 Qt hello world 程序。我在 Windows 64 位上使用 Qt 5.2.0。 这是我的代码

#include <QCoreApplication>
#include <QDebug>

int main(int argc, char *argv[])
{
 QCoreApplication a(argc, argv);
 qDebug() << "Hello world";
 return a.exec();
 }

当我运行应用程序时,出现以下运行时错误:

 mingw32-make[1]: Entering directory 'C:/Users/test/qt_creator_projects/build-QtTest-    TestLocal_PC-Debug'
 g++ -c -pipe -fno-keep-inline-dllexport -g -frtti -Wall -Wextra -fexceptions -mthreads -DUNICODE -DQT_QML_DEBUG -DQT_DECLARATIVE_DEBUG -DQT_CORE_LIB -I..\QtTest -    

 I"D:\Qt\5.2.0\msvc2012_64\include" -I"D:\Qt\5.2.0\msvc2012_64\include\QtCore" -I"debug" -   
 I"." -I"D:\Qt\5.2.0\msvc2012_64\mkspecs\win32-g++" -o debug\main.o ..\QtTest\main.cpp
  g++ -Wl,-subsystem,console -mthreads -o debug\QtTest.exe debug/main.o  -   

  LD:\Qt\5.2.0\msvc2012_64\lib -lQt5Cored 
 debug/main.o: In function `main':
  C:\Users\test\qt_creator_projects\build-QtTest-TestLocal_PC-  
  Debug/../QtTest/main.cpp:5: undefined reference to `_imp___ZN16QCoreApplicationC1ERiPPci'

  C:\Users\test\qt_creator_projects\build-QtTest-TestLocal_PC-  Debug/../QtTest/main.cpp:7:   undefined reference to `_imp___ZN16QCoreApplication4execEv'

  Makefile.Debug:77: recipe for target 'debug\QtTest.exe' failed
  C:\Users\test\qt_creator_projects\build-QtTest-TestLocal_PC-  

  Debug/../QtTest/main.cpp:7: undefined reference to `_imp___ZN16QCoreApplicationD1Ev'
 mingw32-make[1]: Leaving directory 'C:/Users/test/qt_creator_projects/build-QtTest-  TestLocal_PC-Debug'
 Makefile:34: recipe for target 'debug' failed
 C:\Users\test\qt_creator_projects\build-QtTest-TestLocal_PC-Debug/../QtTest/main.cpp:7: undefined reference to `_imp___ZN16QCoreApplicationD1Ev'
  collect2.exe: error: ld returned 1 exit status
 mingw32-make[1]: *** [debug\QtTest.exe] Error 1
mingw32-make: *** [debug] Error 2
16:37:34: The process "D:\pen drive data\MinGW\bin\mingw32-make.exe" exited with code 2.
Error while building/deploying project QtTest (kit: TestLocal PC)
When executing step 'Make'

.pro文件

   QT       += core

   QT       -= gui
   TARGET = QtTest
   CONFIG   += console
   CONFIG   -= app_bundle

   TEMPLATE = app


   SOURCES += main.cpp

我想将 Qt 与 MinGW 一起使用。我目前正在将 Qt creatore 用于 64 位 Visual Studio 2012 编译器。由于 64 位 MinGW 在 5.2.0 的下载中不可用

谁能帮我解决这些引用错误?

最佳答案

如果您没有 VS2012,为什么要下载 VS2012 版本的 Qt?

下载与您拥有的编译器完全匹配的版本。如果没有,请下载源代码并自行编译。

如果你想避免自编译带来的麻烦,那么用Qt开发VS2012 Express完全够用了。它是与付费版本相同的编译器,如果您使用 Qt Creator 进行开发,那么您真的不关心 Express Edition IDE 可能有的任何限制。效果很好。

另一种可能性是使用 Qt 的 mingw 构建。在安装过程中,您可以启用 MinGW 4.8.0 工具链的安装。这为您提供了一个完整的开发环境,包括 IDE、Qt 头文件和二进制文件以及工具链。

关于c++ - 编译Qt hello world程序时出现Undefined reference错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21525905/

相关文章:

c++ - boost optional 和 std::experimental optional assignment 之间的区别

java - C++ 到 Java DepthFirstSearch 的转换

c++ - Visual C++ 2010 速成版 : How to use with Qt?

python - 尝试使用 python 检查多个 qt 单选按钮

c++ - 为什么 opengl 中的 gl_FragCoord.z 与深度缓冲区之一不同?

c++ - 没有 __VA_ARGS__ 的可变参数宏

c++ - C DLL 和 Python CTypes - 发布/调试中的不同返回

c++ - C++跨平台延迟毫秒

c++ - QGraphicsScene 中的小部件

c++ - Qt::如何在Linux中识别/检测系统是否被其他系统远程访问