-1

This question is similar to one that has been asked before, but I am having an issue with Qt recognizing my class and functions despite them being defined. I am doing this in linux where the Qt version is only 5.5 while the windows version is at 5.11. When running the same program in windows, I get no errors.

I am getting "undefined reference to QCanBus::instance()" errors

This is happening for every QCanBus function. In my code snippet from connectdialogue.cpp, only the first occurrence is featured and can be found at the second to last code line and gives the errors:

"undefined reference to QCanBus::instance()"

"undefined reference to QCanBus::plugins()"

These errors are given despite being defined in the qcanbus.h headder file.

I have tried adding the lines static QCanBus *instance() or alternatively QCanBus *instance() under the #include lines in my connectdialogue.cpp, but I am then presented with the warning: "QCanBus::instance() is defined, but unused" when it is clearly used on the second to last line of the featured code snippet.

How do I fix these errors?

connectdialogue.cpp

 #include "connectdialog.h"
 #include "ui_connectdialog.h"

 #include "qcanbus.h"

ConnectDialog::ConnectDialog(QWidget *parent) :
     QDialog(parent),
     m_ui(new Ui::ConnectDialog)
{
m_ui->setupUi(this);

m_ui->errorFilterEdit->setValidator(new QIntValidator(0, 0x1FFFFFFFU, this));

m_ui->loopbackBox->addItem(tr("unspecified"), QVariant());
m_ui->loopbackBox->addItem(tr("false"), QVariant(false));
m_ui->loopbackBox->addItem(tr("true"), QVariant(true));

m_ui->receiveOwnBox->addItem(tr("unspecified"), QVariant());
m_ui->receiveOwnBox->addItem(tr("false"), QVariant(false));
m_ui->receiveOwnBox->addItem(tr("true"), QVariant(true));

m_ui->canFdBox->addItem(tr("false"), QVariant(false));
m_ui->canFdBox->addItem(tr("true"), QVariant(true));

m_ui->dataBitrateBox->setFlexibleDateRateEnabled(true);

connect(m_ui->okButton, &QPushButton::clicked, this, &ConnectDialog::ok);
connect(m_ui->cancelButton, &QPushButton::clicked, this, &ConnectDialog::cancel);
connect(m_ui->useConfigurationBox, &QCheckBox::clicked,
        m_ui->configurationBox, &QGroupBox::setEnabled);
connect(m_ui->backendListBox, &QComboBox::currentTextChanged,
        this, &ConnectDialog::backendChanged);
connect(m_ui->interfaceListBox, &QComboBox::currentTextChanged,
        this, &ConnectDialog::interfaceChanged);
m_ui->rawFilterEdit->hide();
m_ui->rawFilterLabel->hide();

m_ui->backendListBox->addItems(QCanBus::instance()->plugins());

updateSettings();
 }

qcanbus.h

#ifndef QCANBUS_H
#define QCANBUS_H

#include <QtCore/qobject.h>
#include "qserialbusglobal.h"
#include "qcanbusdevice.h"
#include "qcanbusdeviceinfo.h"

QT_BEGIN_NAMESPACE

class Q_SERIALBUS_EXPORT QCanBus : public QObject
{
    Q_OBJECT

public:
    static QCanBus *instance();
    QStringList plugins() const;

    QList<QCanBusDeviceInfo> availableDevices(const QString &plugin, QString *errorMessage = nullptr) const;

    QCanBusDevice *createDevice(const QString &plugin,
                                const QString &interfaceName,
                                QString *errorMessage = nullptr) const;

private:
    QCanBus(QObject *parent = nullptr);

    Q_DISABLE_COPY(QCanBus)
};

QT_END_NAMESPACE

#endif // QSERIALBUS_H
viduwoy
  • 113
  • 12
  • "undefined reference to " usually means you forgot to link the library or object file implementing `foo` (or linked them in the wrong order). – Jesper Juhl Nov 07 '18 at 18:36
  • There are no definitions of those in your header. – molbdnilo Nov 07 '18 at 18:39
  • @molbdnilo where/how would I need to define it? Is the line `static QCanBus *instance();` not defining `QCanBus::instance()`? – viduwoy Nov 07 '18 at 18:48
  • @JesperJuhl that is entirely possible since I am importing missing files in linux from windows. How could I go about finding the missing library or object file? I already have all the dependent `#include` files. – viduwoy Nov 07 '18 at 18:50
  • @viduwoy you could look through the source to find whatever defines the symbol, then look through your build system files to see what that source gets build into. You could just objdump all your object/library files and grep for the symbol in question. You could read documentation to see if it tells you what to link against. There are many ways to find out. – Jesper Juhl Nov 07 '18 at 18:54
  • According to the documentation QCanBus was added to Qt in 5.8 (see https://doc.qt.io/qt-5.11/qcanbus.html) - which means you have to update your Qt (The technical preview was added in 5.6 - but that api is different to the current one, wich means if you want to stay compatible to 5.11, you must use at least 5.8) – Felix Nov 07 '18 at 23:31

1 Answers1

0

QCanBus was missing QCanBus.cpp and was not declared.

viduwoy
  • 113
  • 12