Unresolved external symbols - Qt creator

46,857

Solution 1

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 ?

Solution 2

yes, some time Makefile file is not updated while you change .pro file. So you have to run qmake.

Follow this steps:

  • Right click on project Clean / Build menu -> cleanAll
  • Right click on project Run qmake / Build menu -> runQmake
  • Right click on project Build / Build menu -> build

Pro Advice:

I don't know but sometime Qt is not updating Makefile. so i recomanded to all whenever you add/removing any resource in project or if any changes occur in your .pro file, just Run qmake and build your project(Running qmake do manually to update the path of project, which help to find the mainwindow.obj file).

Share:
46,857
chuckieDub
Author by

chuckieDub

Updated on May 31, 2020

Comments

  • chuckieDub
    chuckieDub about 4 years

    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
    
  • UmNyobe
    UmNyobe over 11 years
    I think this will solve the prolem too. but he need to clean first just to be sure.
  • Rich
    Rich about 11 years
    +1 @benjarobin , After promoting a widget to my own class, then demoting it back then promoting it again, I kept getting unresolved external error and couldn't get rid of it. Coming from VC++ background I thought just a clean and rebuild would do it but no joy. This solved my issue. Many thanks.
  • meakgoz
    meakgoz almost 9 years
    For beginners in windows (like me): 1) Build -> Clean All 2) Build -> Run qmake and then you can build. I was thinking since we are in windows and have comprehensive tool (QT Creator), we do not need to run anything else then "clean" and "build". I also wanted to try typing qmake in cmd, but didnt work for me.
  • Patapoom
    Patapoom about 4 years
    You can do the same with Visual Studio by reloading the .pro.