Qt linker error when adding class to basic Qt GUI application











up vote
1
down vote

favorite












I have been struggling for some time now and can't figure this out. I added a class to my basic Qt GUI application and i got linker errors. I don't see what can cause this and are hoping for feedback.



Linker errors:



main.obj:-1: error: LNK2019: unresolved external symbol "public: void __thiscall Serial::start(enum QSerialPort::BaudRate)" (?start@Serial@@QAEXW4BaudRate@QSerialPort@@@Z) referenced in function _main

main.obj:-1: error: LNK2019: unresolved external symbol "public: __thiscall Serial::Serial(class QObject *)" (??0Serial@@QAE@PAVQObject@@@Z) referenced in function _main

debugTest.exe:-1: error: LNK1120: 2 unresolved externals


Here is the project file:



#-------------------------------------------------
#
# Project created by QtCreator 2013-11-18T10:08:32
#
#-------------------------------------------------

QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = Test
TEMPLATE = app


SOURCES += main.cpp
mainwindow.cpp
serial.cpp

HEADERS += mainwindow.h
serial.h

FORMS += mainwindow.ui


serial.h file:



#ifndef SERIAL_H
#define SERIAL_H

#include <QObject>
#include <QtSerialPort/QSerialPort>

class Serial : public QObject
{
Q_OBJECT
public:
explicit Serial(QObject *parent = 0);
void start(QSerialPort::BaudRate baudRate);
bool Serial::closePort();
void Serial::readData();

signals:

public slots:

};

#endif // SERIAL_H


serial.cpp file:



#include "serial.h"
#include <QObject>
#include <QtSerialPort/QSerialPort>

Serial::Serial(QObject *parent) :
QObject(parent)
{
serial = new QSerialPort(this);
connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
}

bool Serial::start(QSerialPort::BaudRate baudRate)
{
if(serial.open(QIODevice::ReadWrite))
{
//TODO: Connection OK is green in GUI
QDebug << "Port opened successfully" << std::endl;
return true;
}
else
{
//Open failed.
QDebug << "Port failed to open" << std::endl;
//TODO: Show error window in GUI
return false;
}

if(
serial.setBaudRate(baudRate) &&
serial.setDataBits(QSerialPort::Data8) &&
serial.setParity(QSerialPort::NoParity) &&
serial.setStopBits(QSerialPort::OneStop) &&
serial.setFlowControl(QSerialPort::NoFlowControl)
)
{
//Connection ok.
QDebug << "Parameters set OK" << std::endl;
}
else
{
QDebug << "Parameters set FAILED" << std::endl;
Serial::closePort();
}
}

bool Serial::closePort()
{
serial.close();
//TODO: Connection OK is red in GUI
}

void Serial::readData()
{
this->readDataArray = this->serial.readAll();
}









share|improve this question




















  • 2




    What does the error say?
    – Abhishek Bansal
    Nov 18 '13 at 9:28










  • @AbhishekBansal: Thank you for pointing this out. This was awfully sloppy of me!
    – iQt
    Nov 18 '13 at 9:36















up vote
1
down vote

favorite












I have been struggling for some time now and can't figure this out. I added a class to my basic Qt GUI application and i got linker errors. I don't see what can cause this and are hoping for feedback.



Linker errors:



main.obj:-1: error: LNK2019: unresolved external symbol "public: void __thiscall Serial::start(enum QSerialPort::BaudRate)" (?start@Serial@@QAEXW4BaudRate@QSerialPort@@@Z) referenced in function _main

main.obj:-1: error: LNK2019: unresolved external symbol "public: __thiscall Serial::Serial(class QObject *)" (??0Serial@@QAE@PAVQObject@@@Z) referenced in function _main

debugTest.exe:-1: error: LNK1120: 2 unresolved externals


Here is the project file:



#-------------------------------------------------
#
# Project created by QtCreator 2013-11-18T10:08:32
#
#-------------------------------------------------

QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = Test
TEMPLATE = app


SOURCES += main.cpp
mainwindow.cpp
serial.cpp

HEADERS += mainwindow.h
serial.h

FORMS += mainwindow.ui


serial.h file:



#ifndef SERIAL_H
#define SERIAL_H

#include <QObject>
#include <QtSerialPort/QSerialPort>

class Serial : public QObject
{
Q_OBJECT
public:
explicit Serial(QObject *parent = 0);
void start(QSerialPort::BaudRate baudRate);
bool Serial::closePort();
void Serial::readData();

signals:

public slots:

};

#endif // SERIAL_H


serial.cpp file:



#include "serial.h"
#include <QObject>
#include <QtSerialPort/QSerialPort>

Serial::Serial(QObject *parent) :
QObject(parent)
{
serial = new QSerialPort(this);
connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
}

bool Serial::start(QSerialPort::BaudRate baudRate)
{
if(serial.open(QIODevice::ReadWrite))
{
//TODO: Connection OK is green in GUI
QDebug << "Port opened successfully" << std::endl;
return true;
}
else
{
//Open failed.
QDebug << "Port failed to open" << std::endl;
//TODO: Show error window in GUI
return false;
}

if(
serial.setBaudRate(baudRate) &&
serial.setDataBits(QSerialPort::Data8) &&
serial.setParity(QSerialPort::NoParity) &&
serial.setStopBits(QSerialPort::OneStop) &&
serial.setFlowControl(QSerialPort::NoFlowControl)
)
{
//Connection ok.
QDebug << "Parameters set OK" << std::endl;
}
else
{
QDebug << "Parameters set FAILED" << std::endl;
Serial::closePort();
}
}

bool Serial::closePort()
{
serial.close();
//TODO: Connection OK is red in GUI
}

void Serial::readData()
{
this->readDataArray = this->serial.readAll();
}









share|improve this question




















  • 2




    What does the error say?
    – Abhishek Bansal
    Nov 18 '13 at 9:28










  • @AbhishekBansal: Thank you for pointing this out. This was awfully sloppy of me!
    – iQt
    Nov 18 '13 at 9:36













up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have been struggling for some time now and can't figure this out. I added a class to my basic Qt GUI application and i got linker errors. I don't see what can cause this and are hoping for feedback.



Linker errors:



main.obj:-1: error: LNK2019: unresolved external symbol "public: void __thiscall Serial::start(enum QSerialPort::BaudRate)" (?start@Serial@@QAEXW4BaudRate@QSerialPort@@@Z) referenced in function _main

main.obj:-1: error: LNK2019: unresolved external symbol "public: __thiscall Serial::Serial(class QObject *)" (??0Serial@@QAE@PAVQObject@@@Z) referenced in function _main

debugTest.exe:-1: error: LNK1120: 2 unresolved externals


Here is the project file:



#-------------------------------------------------
#
# Project created by QtCreator 2013-11-18T10:08:32
#
#-------------------------------------------------

QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = Test
TEMPLATE = app


SOURCES += main.cpp
mainwindow.cpp
serial.cpp

HEADERS += mainwindow.h
serial.h

FORMS += mainwindow.ui


serial.h file:



#ifndef SERIAL_H
#define SERIAL_H

#include <QObject>
#include <QtSerialPort/QSerialPort>

class Serial : public QObject
{
Q_OBJECT
public:
explicit Serial(QObject *parent = 0);
void start(QSerialPort::BaudRate baudRate);
bool Serial::closePort();
void Serial::readData();

signals:

public slots:

};

#endif // SERIAL_H


serial.cpp file:



#include "serial.h"
#include <QObject>
#include <QtSerialPort/QSerialPort>

Serial::Serial(QObject *parent) :
QObject(parent)
{
serial = new QSerialPort(this);
connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
}

bool Serial::start(QSerialPort::BaudRate baudRate)
{
if(serial.open(QIODevice::ReadWrite))
{
//TODO: Connection OK is green in GUI
QDebug << "Port opened successfully" << std::endl;
return true;
}
else
{
//Open failed.
QDebug << "Port failed to open" << std::endl;
//TODO: Show error window in GUI
return false;
}

if(
serial.setBaudRate(baudRate) &&
serial.setDataBits(QSerialPort::Data8) &&
serial.setParity(QSerialPort::NoParity) &&
serial.setStopBits(QSerialPort::OneStop) &&
serial.setFlowControl(QSerialPort::NoFlowControl)
)
{
//Connection ok.
QDebug << "Parameters set OK" << std::endl;
}
else
{
QDebug << "Parameters set FAILED" << std::endl;
Serial::closePort();
}
}

bool Serial::closePort()
{
serial.close();
//TODO: Connection OK is red in GUI
}

void Serial::readData()
{
this->readDataArray = this->serial.readAll();
}









share|improve this question















I have been struggling for some time now and can't figure this out. I added a class to my basic Qt GUI application and i got linker errors. I don't see what can cause this and are hoping for feedback.



Linker errors:



main.obj:-1: error: LNK2019: unresolved external symbol "public: void __thiscall Serial::start(enum QSerialPort::BaudRate)" (?start@Serial@@QAEXW4BaudRate@QSerialPort@@@Z) referenced in function _main

main.obj:-1: error: LNK2019: unresolved external symbol "public: __thiscall Serial::Serial(class QObject *)" (??0Serial@@QAE@PAVQObject@@@Z) referenced in function _main

debugTest.exe:-1: error: LNK1120: 2 unresolved externals


Here is the project file:



#-------------------------------------------------
#
# Project created by QtCreator 2013-11-18T10:08:32
#
#-------------------------------------------------

QT += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = Test
TEMPLATE = app


SOURCES += main.cpp
mainwindow.cpp
serial.cpp

HEADERS += mainwindow.h
serial.h

FORMS += mainwindow.ui


serial.h file:



#ifndef SERIAL_H
#define SERIAL_H

#include <QObject>
#include <QtSerialPort/QSerialPort>

class Serial : public QObject
{
Q_OBJECT
public:
explicit Serial(QObject *parent = 0);
void start(QSerialPort::BaudRate baudRate);
bool Serial::closePort();
void Serial::readData();

signals:

public slots:

};

#endif // SERIAL_H


serial.cpp file:



#include "serial.h"
#include <QObject>
#include <QtSerialPort/QSerialPort>

Serial::Serial(QObject *parent) :
QObject(parent)
{
serial = new QSerialPort(this);
connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
}

bool Serial::start(QSerialPort::BaudRate baudRate)
{
if(serial.open(QIODevice::ReadWrite))
{
//TODO: Connection OK is green in GUI
QDebug << "Port opened successfully" << std::endl;
return true;
}
else
{
//Open failed.
QDebug << "Port failed to open" << std::endl;
//TODO: Show error window in GUI
return false;
}

if(
serial.setBaudRate(baudRate) &&
serial.setDataBits(QSerialPort::Data8) &&
serial.setParity(QSerialPort::NoParity) &&
serial.setStopBits(QSerialPort::OneStop) &&
serial.setFlowControl(QSerialPort::NoFlowControl)
)
{
//Connection ok.
QDebug << "Parameters set OK" << std::endl;
}
else
{
QDebug << "Parameters set FAILED" << std::endl;
Serial::closePort();
}
}

bool Serial::closePort()
{
serial.close();
//TODO: Connection OK is red in GUI
}

void Serial::readData()
{
this->readDataArray = this->serial.readAll();
}






c++ qt serial-port linker-errors






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 18 '13 at 9:37

























asked Nov 18 '13 at 9:25









iQt

97011342




97011342








  • 2




    What does the error say?
    – Abhishek Bansal
    Nov 18 '13 at 9:28










  • @AbhishekBansal: Thank you for pointing this out. This was awfully sloppy of me!
    – iQt
    Nov 18 '13 at 9:36














  • 2




    What does the error say?
    – Abhishek Bansal
    Nov 18 '13 at 9:28










  • @AbhishekBansal: Thank you for pointing this out. This was awfully sloppy of me!
    – iQt
    Nov 18 '13 at 9:36








2




2




What does the error say?
– Abhishek Bansal
Nov 18 '13 at 9:28




What does the error say?
– Abhishek Bansal
Nov 18 '13 at 9:28












@AbhishekBansal: Thank you for pointing this out. This was awfully sloppy of me!
– iQt
Nov 18 '13 at 9:36




@AbhishekBansal: Thank you for pointing this out. This was awfully sloppy of me!
– iQt
Nov 18 '13 at 9:36












1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










As noted here, because QtSerialPort is an add on module you need to explicitly add it to your project:



QT += serialport





share|improve this answer





















  • Tried it, did not change anything. You might be right though that this is necessary anyways.
    – iQt
    Nov 18 '13 at 9:39






  • 1




    @user1840438 Have you tried to delete your original build files and re-build the project?
    – Abhishek Bansal
    Nov 18 '13 at 9:41






  • 2




    @AbhishekBansal: I tried clean -> rebuild, but that did not solve my problem. I deleted the files and rebuilt and no more linker errors!
    – iQt
    Nov 18 '13 at 10:00










  • For me rather than delete the build files I just used "Build->Run qmake" then "Build->Rebuild All", this worked fine.
    – Preston
    Feb 12 '15 at 21:24











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f20044185%2fqt-linker-error-when-adding-class-to-basic-qt-gui-application%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










As noted here, because QtSerialPort is an add on module you need to explicitly add it to your project:



QT += serialport





share|improve this answer





















  • Tried it, did not change anything. You might be right though that this is necessary anyways.
    – iQt
    Nov 18 '13 at 9:39






  • 1




    @user1840438 Have you tried to delete your original build files and re-build the project?
    – Abhishek Bansal
    Nov 18 '13 at 9:41






  • 2




    @AbhishekBansal: I tried clean -> rebuild, but that did not solve my problem. I deleted the files and rebuilt and no more linker errors!
    – iQt
    Nov 18 '13 at 10:00










  • For me rather than delete the build files I just used "Build->Run qmake" then "Build->Rebuild All", this worked fine.
    – Preston
    Feb 12 '15 at 21:24















up vote
3
down vote



accepted










As noted here, because QtSerialPort is an add on module you need to explicitly add it to your project:



QT += serialport





share|improve this answer





















  • Tried it, did not change anything. You might be right though that this is necessary anyways.
    – iQt
    Nov 18 '13 at 9:39






  • 1




    @user1840438 Have you tried to delete your original build files and re-build the project?
    – Abhishek Bansal
    Nov 18 '13 at 9:41






  • 2




    @AbhishekBansal: I tried clean -> rebuild, but that did not solve my problem. I deleted the files and rebuilt and no more linker errors!
    – iQt
    Nov 18 '13 at 10:00










  • For me rather than delete the build files I just used "Build->Run qmake" then "Build->Rebuild All", this worked fine.
    – Preston
    Feb 12 '15 at 21:24













up vote
3
down vote



accepted







up vote
3
down vote



accepted






As noted here, because QtSerialPort is an add on module you need to explicitly add it to your project:



QT += serialport





share|improve this answer












As noted here, because QtSerialPort is an add on module you need to explicitly add it to your project:



QT += serialport






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 18 '13 at 9:35









cmannett85

16.6k54781




16.6k54781












  • Tried it, did not change anything. You might be right though that this is necessary anyways.
    – iQt
    Nov 18 '13 at 9:39






  • 1




    @user1840438 Have you tried to delete your original build files and re-build the project?
    – Abhishek Bansal
    Nov 18 '13 at 9:41






  • 2




    @AbhishekBansal: I tried clean -> rebuild, but that did not solve my problem. I deleted the files and rebuilt and no more linker errors!
    – iQt
    Nov 18 '13 at 10:00










  • For me rather than delete the build files I just used "Build->Run qmake" then "Build->Rebuild All", this worked fine.
    – Preston
    Feb 12 '15 at 21:24


















  • Tried it, did not change anything. You might be right though that this is necessary anyways.
    – iQt
    Nov 18 '13 at 9:39






  • 1




    @user1840438 Have you tried to delete your original build files and re-build the project?
    – Abhishek Bansal
    Nov 18 '13 at 9:41






  • 2




    @AbhishekBansal: I tried clean -> rebuild, but that did not solve my problem. I deleted the files and rebuilt and no more linker errors!
    – iQt
    Nov 18 '13 at 10:00










  • For me rather than delete the build files I just used "Build->Run qmake" then "Build->Rebuild All", this worked fine.
    – Preston
    Feb 12 '15 at 21:24
















Tried it, did not change anything. You might be right though that this is necessary anyways.
– iQt
Nov 18 '13 at 9:39




Tried it, did not change anything. You might be right though that this is necessary anyways.
– iQt
Nov 18 '13 at 9:39




1




1




@user1840438 Have you tried to delete your original build files and re-build the project?
– Abhishek Bansal
Nov 18 '13 at 9:41




@user1840438 Have you tried to delete your original build files and re-build the project?
– Abhishek Bansal
Nov 18 '13 at 9:41




2




2




@AbhishekBansal: I tried clean -> rebuild, but that did not solve my problem. I deleted the files and rebuilt and no more linker errors!
– iQt
Nov 18 '13 at 10:00




@AbhishekBansal: I tried clean -> rebuild, but that did not solve my problem. I deleted the files and rebuilt and no more linker errors!
– iQt
Nov 18 '13 at 10:00












For me rather than delete the build files I just used "Build->Run qmake" then "Build->Rebuild All", this worked fine.
– Preston
Feb 12 '15 at 21:24




For me rather than delete the build files I just used "Build->Run qmake" then "Build->Rebuild All", this worked fine.
– Preston
Feb 12 '15 at 21:24


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f20044185%2fqt-linker-error-when-adding-class-to-basic-qt-gui-application%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin