【问题标题】:Cannot build project from QT wiki: Unresolved external symbol无法从 QT wiki 构建项目:未解析的外部符号
【发布时间】:2018-08-04 16:42:07
【问题描述】:

尝试构建我直接从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();
}

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

【问题讨论】:

  • 首先,不要对错误进行截图,复制/粘贴。其次,您使用的是哪个版本的Qt
  • Qt Creator 4.5.1(社区)
  • Qt 是否安装正确?哪个版本?你能在上面运行其他的吗?
  • 我认为 QT 是 5.10.0 版。我已经成功地在上面构建了其他项目。

标签: c++ qt qtgui


【解决方案1】:

Solution From a similar thread.

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

为什么会起作用

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

【讨论】:

    猜你喜欢
    • 2016-10-13
    • 2012-12-13
    • 1970-01-01
    • 1970-01-01
    • 2013-11-10
    • 1970-01-01
    • 2022-08-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多