Unresolved external symbols - Qt creator

chuckieDub picture chuckieDub · Dec 28, 2012 · Viewed 44.7k times · Source

I must be missing a basic concept with headers and includes because when I attempt to call even the simplest of a function from a separate source file I get an error:

main.obj:-1: error: LNK2019: unresolved external symbol "void __cdecl buildDeck(int,int)" (?buildDeck@@YAXHH@Z) referenced in function _main

deck.h

#ifndef DECK_H
#define DECK_H
#include <QString>


void buildDeck(int deckSize, int jokers);


struct card
{
    QString suit;
    QString color;
    int rank;
};

#endif // DECK_H

deck.cpp

#include"mainwindow.h"
#include "deck.h"

void buildDeck(int deckSize, int jokers)
{
    int blackRed = deckSize-=jokers;
}

main.cpp

#include "mainwindow.h"
#include "deck.h"
#include <QApplication>
#include <QPushButton>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    buildDeck(10,20);

    return a.exec();
}

And this gives me an error. However, If I move the function definition from deck.cpp to the bottom of main.cpp, then the application will build.

All of the files are included in the same project, and stored in the same directory.

Other files:

.pro file

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = carddeck
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp \
    deck.cpp

HEADERS  += mainwindow.h \
    deck.h

FORMS    += mainwindow.ui

not sure if you need it, but here's mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QPushButton>
#include <QTextEdit>
#include <QCheckBox>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void runButtonClicked();

private:
    Ui::MainWindow *ui;
    QPushButton *runButton;
    QTextEdit * runText;

    QCheckBox * betHearts;
    QCheckBox * betDiamonds ;
    QCheckBox * betClubs;
    QCheckBox * betSpades ;
    QCheckBox * betFlush ;
    QCheckBox * betAllRed;
    QCheckBox * betAllBlack ;

};

#endif // MAINWINDOW_H

Answer

benjarobin picture benjarobin · Dec 28, 2012

It looks like the Makefile was not regenerated when you edited the .pro file. Run qmake and try again. You could also check if the deck.cpp is compiled or not; is there a deck.o in the build directory ?