无法从 QT wiki 构建项目:未解析的外部符号

Cannot build project from QT wiki: Unresolved external symbol

尝试构建我直接从 QT for beginners wiki page.

复制粘贴的代码时出错

错误

main.obj:-1: error: LNK2019: unresolved external symbol "public: __cdecl Window::Window(class QWidget *)" (??0Window@@QEAA@PEAVQWidget@@@Z) referenced in function main
debug\testempty.exe:-1: error: LNK1120: 1 unresolved externals

testempty.pro

TEMPLATE = app
TARGET = testempty
QT = core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
SOURCES += \
    main.cpp \
    window.cpp
HEADERS += \
    window.h

window.h

#ifndef WINDOW_H
#define WINDOW_H

#include <QWidget>

class QPushButton;
class Window : public QWidget
{
public:
    explicit Window(QWidget *parent = nullptr);
private:
    QPushButton *m_button;
};

#endif // WINDOW_H

window.cpp

#include "window.h"
#include <QPushButton>

Window::Window(QWidget *parent) : QWidget(parent)
{
    setFixedSize(100, 50);

    m_button = new QPushButton("Hello World", this);
    m_button->setGeometry(10, 10, 80, 30);
}

main.cpp

#include <QApplication>
#include "window.h"

int main(int argc, char **argv)
{
    QApplication app(argc, argv);
    Window window;
    window.show();

    return app.exec();
}

至于我已经尝试过的事情,我被指示构建->清理所有并再次尝试构建,但这没有任何区别。

Solution From a similar thread.

  1. 右键单击项目 > 清理
  2. 右键单击项目 > 运行 qmake
  3. 右键单击项目 > 构建
  4. 运行

为什么有效

之所以有效,是因为 运行 qmake 更新了您的 Makefile。由于某些原因,当您对项目进行更改(例如添加或删除文件)时,qt 不会自动更新路径。 运行ning qmake 强制 qt 更新项目的路径,使其能够找到 mainwindow.obj 文件。您可能只需 运行 qmake 即可解决您的问题。