我在项目构建时遇到类似
error: undefined reference to __imp__ZN12QApplicationC1ERiPPci'
和 error: undefined reference to __imp__ZN11QMainWindow11qt_metacastEPKc'
的错误。我尝试过清理和重建,但没有任何效果。
我看过很多关于此错误的其他帖子,但没有修复方法有效。我是 Qt 创建者的新手,所以我可能在这里错过了一些大东西,但我做了一个小部件项目。以下是文件:
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
主窗口.cpp
#include "mainwindow.h"
#include <QPushButton>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setFixedSize(500,500);
QPushButton myButton;
myButton.setText("Hello world, this is a GUI app!!!");
}
MainWindow::~MainWindow()
{
}
主窗口.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
~MainWindow();
};
#endif // MAINWINDOW_H
myWindow.pro
#-------------------------------------------------
#
# Project created by QtCreator 2018-03-11T19:52:19
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = myWindow
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
QT += sql #added because I found this to try and solve my problem, doesn't fix
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
我的Qt版本5.10.1,操作系统是Windows,编译器是MinGW 64。
好吧,我自己修好了,所以这是我搞砸的两件事:
扩展
QWidget
而不是 QMainWindow
检查我的套件并使用具有正确版本 MinGW 的套件 - 不是 64 位或 32 位,它被称为完全不同的东西,但名称中仍然有 MinGW;)
修复是通过安装您正在使用的软件包的适当版本来完成的。一些新的代码技术可能是一个难题,因此安装正确类型的包可能很困难,但有时是必需的。
我最近偶然发现了这个问题,因为我花了很长时间才找到解决方案,所以我决定在这里添加这个答案。
undefinded reference to __imp_
在我的例子中是一个在我的编译和链接命令中缺少链接选项的指示器。gcc -LC:/myproject/libs -o executable_file.exe source_file.c -llibusb0 -lstcommdll
我假设其他编译器也有等效的东西。