Results 1 to 3 of 3

Thread: Undefined reference to function if in .cpp file

  1. #1
    Join Date
    May 2020
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Undefined reference to function if in .cpp file

    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:
    Qt Code:
    1. #ifndef PROGRESSUPDATOR_H
    2. #define PROGRESSUPDATOR_H
    3.  
    4. #include <QObject>
    5.  
    6. class ProgressUpdator : public QObject
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit ProgressUpdator(QObject *parent = nullptr);
    11.  
    12. signals:
    13. void updateProgress(int progress);
    14.  
    15. public:
    16. void setProgress(int progress);
    17. };
    18.  
    19. #endif // PROGRESSUPDATOR_H
    To copy to clipboard, switch view to plain text mode 

    with my .cpp file being:
    Qt Code:
    1. #include "progressupdator.h"
    2.  
    3. ProgressUpdator::ProgressUpdator(QObject *parent) : QObject(parent)
    4. {}
    5.  
    6. void ProgressUpdator::setProgress(int progress)
    7. {
    8. emit updateProgress(progress);
    9. }
    To copy to clipboard, switch view to plain text mode 

    This is called from a class performing the algorithm
    Qt Code:
    1. for (int i=0; i<100; ++i)
    2. {
    3. // Do work
    4. progressUpdator->setProgress(i);
    5. }
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,719
    Thanks
    259
    Thanked 760 Times in 750 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Undefined reference to function if in .cpp file

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,677
    Thanks
    13
    Thanked 1,596 Times in 1,524 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Undefined reference to function if in .cpp file

    Quote Originally Posted by Cheesy135 View Post
    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.

Similar Threads

  1. undefined reference to function
    By mania in forum Qt Programming
    Replies: 1
    Last Post: 1st December 2014, 09:00
  2. Undefined reference
    By eekhoorn12 in forum Qt Programming
    Replies: 2
    Last Post: 6th January 2011, 16:45
  3. undefined reference
    By digidas in forum Newbie
    Replies: 9
    Last Post: 19th May 2010, 14:04
  4. Undefined Reference To...
    By ManuMies in forum Qt Programming
    Replies: 6
    Last Post: 10th February 2009, 13:14
  5. Undefined reference
    By Salazaar in forum Newbie
    Replies: 12
    Last Post: 23rd May 2007, 11:21

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.