c++ - qt creator 中有很多 undefined reference

标签 c++ qt mingw32

我正在使用 Win7 32 位、MinGW 和 Qt 5.7.0。我是初学者,想学习一些 GUI 的东西。执行它时出现数百个 undefined reference 错误。 这是我的 .pro 文件。

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = new
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

我在函数中只写了一行代码:

void MainWindow::on_pushButton_clicked()
{
    ui->label->setText("lkj");
}

我得到如下完整的错误:

C:\Users\xxxxx\Documents\new\main.cpp:6: error: undefined reference to _imp___ZN12QApplicationC1ERiPPci
C:\Users\xxxxx\Documents\new\main.cpp:8: error: undefined reference to _imp___ZN7QWidget4showEv
C:\Users\xxxxx\Documents\new\main.cpp:10: error: undefined reference to _imp___ZN12QApplication4execEv

仅这一行就有数百个这样的错误。

不知道是什么问题。 编辑:编译输出如下

C:\Qt\Qt5.7.0\5.7\msvc2015\bin\qmake.exe -spec win32-g++ "CONFIG+=debug" "CONFIG+=qml_debug" -o Makefile ..\new\new.pro
C:/Program Files/mingw-w64/i686-6.1.0-posix-dwarf-rt_v5-rev1/mingw32/bin/mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory 'C:/Users/Anant/Documents/build-new-Clone_of_Desktop_Qt_5_7_0_MSVC2015_32bit-Debug'
g++ -Wl,-subsystem,windows -mthreads -o debug\new.exe debug/main.o debug/mainwindow.o debug/moc_mainwindow.o  -lmingw32 -LC:\Qt\Qt5.7.0\5.7\msvc2015\lib C:\Qt\Qt5.7.0\5.7\msvc2015\lib\qtmaind.lib -LC:\utils\postgresql\pgsql\lib -LC:\utils\my_sql\my_sql\lib -lshell32 C:\Qt\Qt5.7.0\5.7\msvc2015\lib\Qt5Widgetsd.lib C:\Qt\Qt5.7.0\5.7\msvc2015\lib\Qt5Guid.lib C:\Qt\Qt5.7.0\5.7\msvc2015\lib\Qt5Cored.lib 
Warning: corrupt .drectve at end of def file
debug/main.o: In function `Z5qMainiPPc':
C:\Users\Anant\Documents\build-new-Clone_of_Desktop_Qt_5_7_0_MSVC2015_32bit-Debug/../new/main.cpp:6: undefined reference to `_imp___ZN12QApplicationC1ERiPPci'
C:\Users\Anant\Documents\build-new-Clone_of_Desktop_Qt_5_7_0_MSVC2015_32bit-Debug/../new/main.cpp:8: undefined reference to `_imp___ZN7QWidget4showEv'
C:\Users\Anant\Documents\build-new-Clone_of_Desktop_Qt_5_7_0_MSVC2015_32bit-Debug/../new/main.cpp:10: undefined reference to `_imp___ZN12QApplication4execEv'
C:\Users\Anant\Documents\build-new-Clone_of_Desktop_Qt_5_7_0_MSVC2015_32bit-Debug/../new/main.cpp:6: undefined reference to `_imp___ZN12QApplicationD1Ev'
C:\Users\Anant\Documents\build-new-Clone_of_Desktop_Qt_5_7_0_MSVC2015_32bit-Debug/../new/main.cpp:6: undefined reference to `_imp___ZN12QApplicationD1Ev'
debug/mainwindow.o: In function `ZN10MainWindowC2EP7QWidget':
C:\Users\Anant\Documents\build-new-Clone_of_Desktop_Qt_5_7_0_MSVC2015_32bit-Debug/../new/mainwindow.cpp:6: undefined reference to `_imp___ZN11QMainWindowC2EP7QWidget6QFlagsIN2Qt10WindowTypeEE'
C:\Users\Anant\Documents\build-new-Clone_of_Desktop_Qt_5_7_0_MSVC2015_32bit-Debug/../new/mainwindow.cpp:6: undefined reference to `_imp___ZN11QMainWindowD2Ev'
debug/mainwindow.o: In function `ZN10MainWindowD2Ev':
C:\Users\Anant\Documents\build-new-Clone_of_Desktop_Qt_5_7_0_MSVC2015_32bit-Debug/../new/mainwindow.cpp:11: undefined reference to `_imp___ZN11QMainWindowD2Ev'
debug/mainwindow.o: In function `ZN10MainWindow21on_pushButton_clickedEv':
C:\Users\Anant\Documents\build-new-Clone_of_Desktop_Qt_5_7_0_MSVC2015_32bit-Debug/../new/mainwindow.cpp:18: undefined reference to `_imp___ZN6QLabel7setTextERK7QString'
debug/mainwindow.o: In function `ZN7QStringC1EPKc':
C:/Qt/Qt5.7.0/5.7/msvc2015/include/QtCore/qstring.h:664: undefined reference to `_imp___ZN7QString16fromAscii_helperEPKci'
debug/mainwindow.o: In function `ZN7QWidget6resizeEii':
C:/Qt/Qt5.7.0/5.7/msvc2015/include/QtWidgets/qwidget.h:835: undefined reference to `_imp___ZN7QWidget6resizeERK5QSize'
debug/mainwindow.o: In function `ZN13Ui_MainWindow7setupUiEP11QMainWindow':
C:\Users\Anant\Documents\build-new-Clone_of_Desktop_Qt_5_7_0_MSVC2015_32bit-Debug/./ui_mainwindow.h:39: undefined reference to `_imp___ZNK7QObject10objectNameEv'
C:\Users\Anant\Documents\build-new-Clone_of_Desktop_Qt_5_7_0_MSVC2015_32bit-Debug/./ui_mainwindow.h:40: undefined reference to `_imp___ZN7QObject13setObjectNameERK7QString'
C:\Users\Anant\Documents\build-new-Clone_of_Desktop_Qt_5_7_0_MSVC2015_32bit-Debug/./ui_mainwindow.h:42: undefined reference to `_imp___ZN7QWidgetC1EPS_6QFlagsIN2Qt10WindowTypeEE'
C:\Users\Anant\Documents\build-new-Clone_of_Desktop_Qt_5_7_0_MSVC2015_32bit-Debug/./ui_mainwindow.h:43: undefined reference to `_imp___ZN7QObject13setObjectNameERK7QString'
C:\Users\Anant\Documents\build-new-Clone_of_Desktop_Qt_5_7_0_MSVC2015_32bit-Debug/./ui_mainwindow.h:44: undefined reference to `_imp___ZN11QPushButtonC1EP7QWidget'
C:\Users\Anant\Documents\build-new-Clone_of_Desktop_Qt_5_7_0_MSVC2015_32bit-Debug/./ui_mainwindow.h:45: undefined reference to `_imp___ZN7QObject13setObjectNameERK7QString'
C:\Users\Anant\Documents\build-new-Clone_of_Desktop_Qt_5_7_0_MSVC2015_32bit-Debug/./ui_mainwindow.h:46: undefined reference to `_imp___ZN7QWidget11setGeometryERK5QRect'
C:\Users\Anant\Documents\build-new-Clone_of_Desktop_Qt_5_7_0_MSVC2015_32bit-Debug/./ui_mainwindow.h:47: undefined reference to `_imp___ZN6QLabelC1EP7QWidget6QFlagsIN2Qt10WindowTypeEE'
C:\Users\Anant\Documents\build-new-Clone_of_Desktop_Qt_5_7_0_MSVC2015_32bit-Debug/./ui_mainwindow.h:48: undefined reference to `_imp___ZN7QObject13setObjectNameERK7QString'
C:\Users\Anant\Documents\build-new-Clone_of_Desktop_Qt_5_7_0_MSVC2015_32bit-Debug/./ui_mainwindow.h:49: undefined reference to `_imp___ZN7QWidget11setGeometryERK5QRect'
C:\Users\Anant\Documents\build-new-Clone_of_Desktop_Qt_5_7_0_MSVC2015_32bit-Debug/./ui_mainwindow.h:50: undefined reference to `_imp___ZN11QMainWindow16setCentralWidgetEP7QWidget'
C:\Users\Anant\Documents\build-new-Clone_of_Desktop_Qt_5_7_0_MSVC2015_32bit-Debug/./ui_mainwindow.h:51: undefined reference to `_imp___ZN8QMenuBarC1EP7QWidget'
C:\Users\Anant\Documents\build-new-Clone_of_Desktop_Qt_5_7_0_MSVC2015_32bit-Debug/./ui_mainwindow.h:52: undefined reference to `_imp___ZN7QObject13setObjectNameERK7QString'
C:\Users\Anant\Documents\build-new-Clone_of_Desktop_Qt_5_7_0_MSVC2015_32bit-Debug/./ui_mainwindow.h:53: undefined reference to `_imp___ZN7QWidget11setGeometryERK5QRect'
C:\Users\Anant\Documents\build-new-Clone_of_Desktop_Qt_5_7_0_MSVC2015_32bit-Debug/./ui_mainwindow.h:54: undefined reference to `_imp___ZN11QMainWindow10setMenuBarEP8QMenuBar'
C:\Users\Anant\Documents\build-new-Clone_of_Desktop_Qt_5_7_0_MSVC2015_32bit-Debug/./ui_mainwindow.h:55: undefined reference to `_imp___ZN8QToolBarC1EP7QWidget'
C:\Users\Anant\Documents\build-new-Clone_of_Desktop_Qt_5_7_0_MSVC2015_32bit-Debug/./ui_mainwindow.h:56: undefined reference to `_imp___ZN7QObject13setObjectNameERK7QString'
C:\Users\Anant\Documents\build-new-Clone_of_Desktop_Qt_5_7_0_MSVC2015_32bit-Debug/./ui_mainwindow.h:57: undefined reference to `_imp___ZN11QMainWindow10addToolBarEN2Qt11ToolBarAreaEP8QToolBar'
C:\Users\Anant\Documents\build-new-Clone_of_Desktop_Qt_5_7_0_MSVC2015_32bit-Debug/./ui_mainwindow.h:58: undefined reference to `_imp___ZN10QStatusBarC1EP7QWidget'
C:\Users\Anant\Documents\build-new-Clone_of_Desktop_Qt_5_7_0_MSVC2015_32bit-Debug/./ui_mainwindow.h:59: undefined reference to `_imp___ZN7QObject13setObjectNameERK7QString'
C:\Users\Anant\Documents\build-new-Clone_of_Desktop_Qt_5_7_0_MSVC2015_32bit-Debug/./ui_mainwindow.h:60: undefined reference to `_imp___ZN11QMainWindow12setStatusBarEP10QStatusBar'
C:\Users\Anant\Documents\build-new-Clone_of_Desktop_Qt_5_7_0_MSVC2015_32bit-Debug/./ui_mainwindow.h:64: undefined reference to `_imp___ZN11QMetaObject18connectSlotsByNameEP7QObject'
debug/mainwindow.o: In function `ZN13Ui_MainWindow13retranslateUiEP11QMainWindow':
C:\Users\Anant\Documents\build-new-Clone_of_Desktop_Qt_5_7_0_MSVC2015_32bit-Debug/./ui_mainwindow.h:69: undefined reference to `_imp___ZN16QCoreApplication9translateEPKcS1_S1_i'
C:\Users\Anant\Documents\build-new-Clone_of_Desktop_Qt_5_7_0_MSVC2015_32bit-Debug/./ui_mainwindow.h:69: undefined reference to `_imp___ZN7QWidget14setWindowTitleERK7QString'
C:\Users\Anant\Documents\build-new-Clone_of_Desktop_Qt_5_7_0_MSVC2015_32bit-Debug/./ui_mainwindow.h:70: undefined reference to `_imp___ZN16QCoreApplication9translateEPKcS1_S1_i'
C:\Users\Anant\Documents\build-new-Clone_of_Desktop_Qt_5_7_0_MSVC2015_32bit-Debug/./ui_mainwindow.h:70: undefined reference to `_imp___ZN15QAbstractButton7setTextERK7QString'
C:\Users\Anant\Documents\build-new-Clone_of_Desktop_Qt_5_7_0_MSVC2015_32bit-Debug/./ui_mainwindow.h:71: undefined reference to `_imp___ZN16QCoreApplication9translateEPKcS1_S1_i'
C:\Users\Anant\Documents\build-new-Clone_of_Desktop_Qt_5_7_0_MSVC2015_32bit-Debug/./ui_mainwindow.h:71: undefined reference to `_imp___ZN6QLabel7setTextERK7QString'
debug/mainwindow.o: In function `ZN15QTypedArrayDataItE10deallocateEP10QArrayData':
C:/Qt/Qt5.7.0/5.7/msvc2015/include/QtCore/qarraydata.h:228: undefined reference to `_imp___ZN10QArrayData10deallocateEPS_jj'
debug/mainwindow.o: In function `ZNK17QStaticStringDataILi10EE8data_ptrEv':
C:/Qt/Qt5.7.0/5.7/msvc2015/include/QtCore/qstring.h:207: undefined reference to `_imp___Z9qt_assertPKcS0_i'
debug/mainwindow.o: In function `ZNK17QStaticStringDataILi13EE8data_ptrEv':
...

最佳答案

您的问题可能是您混合使用了 MinGW 和 MSVC。看这里:

C:\Qt\Qt5.7.0\5.7\msvc2015\bin\qmake.exe -spec win32-g++

在这里:

C:\Qt\Qt5.7.0\5.7\msvc2015\lib\Qt5Widgetsd.lib

当然,MinGW g++ 不能链接到 MSVC 库。它们具有不同的二进制格式和命名约定。最简单的处理方法是安装单一版本的 Qt。如果您想要 MinGW,请下载 MinGW 二进制文件。它们与 MinGW 版本捆绑在一起,可以独立运行。

关于c++ - qt creator 中有很多 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39063110/

相关文章:

javascript - 使用 QtWebEngine 在 Qt 中将 C++ 对象暴露给 Javascript

c++ - QT中QByteArray和QByte Stream如何释放内存

c++ - 我正在尝试安装 minGW,但我的环境路径不同,我无法编辑?

c++ ->= 优先级高于 | 的直观原因

c++ - 无法使用 Visual Studio 和 CMake 将 __cplusplus 设置为 C++17 标准

c++ - 数组/指针列表 C++

qt - 检测 QPushButton 是否被单击

C++ 多重继承内存寻址问题

c++ - -O1/2/3 与 -std=c++1y/11/98 - 如果包含 <cmath> 我收到错误 : '_hypot' was not declared in this scope

c++ - 查找下一个具有真值的数组索引 C++