-2

Error when attempting to build code I copy-pasted directly from the QT for beginners wiki page.

error

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();
}

As for things I have already tried, I was instructed to build->clean all and try building again, but that made no difference.

Corvus
  • 53
  • 9

1 Answers1

1

Solution From a similar thread.

  1. Right click on project > Clean
  2. Right click on project > Run qmake
  3. Right click on project > Build
  4. Run

Why it works

The reason this worked is because Run qmake updates your Makefile. For some reason qt is not automatically updating paths when you make changes to your project such as adding or removing files. Running qmake forces qt to update the paths for your project which enables it to find the mainwindow.obj file. You probably could just run qmake and your problem would be solved.

Corvus
  • 53
  • 9