Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unresolved external symbols - Qt creator

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 
like image 304
chuckieDub Avatar asked Dec 28 '12 11:12

chuckieDub


2 Answers

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 ?

like image 196
benjarobin Avatar answered Oct 05 '22 20:10

benjarobin


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).

like image 25
Kartik Maheshwari Avatar answered Oct 05 '22 22:10

Kartik Maheshwari