PDA

View Full Version : Undefined reference to function if in .cpp file



Cheesy135
24th May 2020, 07:35
Hi All,

I feel like this should be simple I am just not sure what is going on.

I've got a ProgressUpdator function used to handle the signal updates of a progress bar:


#ifndef PROGRESSUPDATOR_H
#define PROGRESSUPDATOR_H

#include <QObject>

class ProgressUpdator : public QObject
{
Q_OBJECT
public:
explicit ProgressUpdator(QObject *parent = nullptr);

signals:
void updateProgress(int progress);

public:
void setProgress(int progress);
};

#endif // PROGRESSUPDATOR_H


with my .cpp file being:


#include "progressupdator.h"

ProgressUpdator::ProgressUpdator(QObject *parent) : QObject(parent)
{}

void ProgressUpdator::setProgress(int progress)
{
emit updateProgress(progress);
}


This is called from a class performing the algorithm


for (int i=0; i<100; ++i)
{
// Do work
progressUpdator->setProgress(i);
}


Any my .pro file has all files added to HEADERS and SOURCES as needed.

But this comes up with a undefined reference to setProgress().

If I try and get around this and put the contents of setProgress in the header file as an inline function the linker then complains that it cannot find the reference to updateProgress instead.

I've also tried running qmake and rebuilding but this doesn't make a difference.

Thanks in advance.

d_stranz
24th May 2020, 15:57
You're including "progressupdator.h" in the place where you are using the ProgressUpdator class, right? Try manually deleting all of the output and generated files and doing a clean qmake / rebuild.

I don't use Qt Creator much, but if there is a verbose mode you can turn on for the build, that might tell you if the compiled object files for ProgressUpdator are actually being linked in.

ChrisW67
26th May 2020, 23:09
But this comes up with a undefined reference to setProgress().

OK, if the linker is complaining then the object file containing the function is either not being included in the link, or is included but does not export the function (could be an old version for example). Hard to tell without the actual compile/link output.