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
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 ?
yes, some time Makefile file is not updated while you change .pro file. So you have to run qmake.
Follow this steps:
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With