Results 1 to 6 of 6

Thread: Trying to get basic signals example working

  1. #1
    Join Date
    Feb 2007
    Posts
    3
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Trying to get basic signals example working

    Hi there--I'm just now learning Qt, and am trying to get a basic signals program working. It compiles, but links unsuccessfully (the compiler complains about the virtual table). I built the program with the standard qmake -project, qmake, make instructions. Does anyone have any ideas on this?

    Here's the code, followed by the compiler output:

    Qt Code:
    1. #include <QtCore/QObject>
    2. #include <iostream>
    3.  
    4. class Go : public QObject
    5. {
    6. Q_OBJECT
    7.  
    8. public:
    9.  
    10. Go() { ; }
    11.  
    12. ~Go() { ; }
    13.  
    14. void go()
    15. {
    16. std::cout << "Emitting signal..." << std::endl;
    17. emit emit_signal();
    18. }
    19.  
    20. public slots:
    21. void receive_signal()
    22. {
    23. std::cout << "Received signal!" << std::endl;
    24. }
    25.  
    26. signals:
    27. void emit_signal() { }
    28.  
    29. };
    30.  
    31. int main()
    32. {
    33. Go g1, g2;
    34.  
    35. QObject::connect(&g1, SIGNAL (emit_signal ()),
    36. &g2, SLOT (receive_signal ()));
    37.  
    38. g1.go();
    39.  
    40. return 0;
    41. }
    To copy to clipboard, switch view to plain text mode 

    [Compiler output]
    g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/local/qt/mkspecs/linux-g++ -I. -I/usr/local/qt42/include/QtCore -I/usr/local/qt42/include/QtCore -I/usr/local/qt42/include/QtGui -I/usr/local/qt42/include/QtGui -I/usr/local/qt42/include -I. -I. -I. -o qtest.o qtest.C
    g++ -o qtest qtest.o -L/usr/local/qt42/lib -lQtGui -L/usr/local/qt/lib -L/usr/X11R6/lib -lpng -lSM -lICE -lXrender -lXrandr -lXfixes -lXcursor -lXinerama -lfreetype -lfontconfig -lXext -lX11 -lQtCore -lz -lm -lglib-2.0 -ldl -lpthread
    qtest.o(.text+0x24): In function `main':
    : undefined reference to `vtable for Go'
    qtest.o(.text+0x43): In function `main':
    : undefined reference to `vtable for Go'
    qtest.o(.text+0x6a): In function `main':
    : undefined reference to `vtable for Go'
    qtest.o(.text+0x7c): In function `main':
    : undefined reference to `vtable for Go'
    qtest.o(.text+0x9d): In function `main':
    : undefined reference to `vtable for Go'
    qtest.o(.text+0xb0): more undefined references to `vtable for Go' follow
    collect2: ld returned 1 exit status
    make: *** [qtest] Error 1

    Thanks!
    --Steve (sgross@sjm.com)
    Last edited by wysota; 27th February 2007 at 20:28. Reason: missing [code] tags

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Trying to get basic signals example working

    Hi,

    Q_OBJECT macro works flawlessly in header files, but needs a little trick when added into a source file:
    1. please rename qtest.C -> qtest.cpp (I've seen people having problems with the ".c" extension)
    2. add #include "qtext.moc" at the end of the qtest.cpp
    3. re-run qmake -project, qmake and make

    Usually "qmake -project" is run only once in the beginning to create an initial project file. In this case it can be done again because there is only one file and it has been renamed. "qmake" needs to be re-run always after adding Q_OBJECT macro(s).
    J-P Nurmi

  3. The following user says thank you to jpn for this useful post:

    mrstephengross (27th February 2007)

  4. #3
    Join Date
    Feb 2007
    Posts
    73
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Trying to get basic signals example working

    Actually, I use .C extension all the time. And when I have seen this, I've cleared it by re-running qmake, then make. This seems to put in all the necessary moc creation into the makefile. I never have to include *.moc in my sources, rather they get linked in instead.

    HTH,
    Susan

  5. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Trying to get basic signals example working

    Quote Originally Posted by smacchia View Post
    Actually, I use .C extension all the time.
    I'm not exactly sure if this has just been a problem in the past and already corrected. But I'm certainly sure that there have been many people for whom qmake failed to create correct makefiles, because of the extension.

    Quote Originally Posted by smacchia View Post
    And when I have seen this, I've cleared it by re-running qmake, then make. This seems to put in all the necessary moc creation into the makefile. I never have to include *.moc in my sources, rather they get linked in instead.
    Yes, there is no problem when the macro is in a header file and the header is listed in .pro file. Furthermore, the "vtable" problem has nothing to do with the file extension. I was just assuring the correct behavior of the tools because I've seen it fail with such extension so many times.

    Edit: Heh, how embarrassing. I didn't remember that the problem was so simple. A filename having ".c" as an extension gets compiled with gcc instead of g++. However, this is not the case with the uppercase version ".C", so there's no problem. Just for your information..
    Last edited by jpn; 27th February 2007 at 17:54.
    J-P Nurmi

  6. #5
    Join Date
    Feb 2007
    Posts
    73
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: Trying to get basic signals example working

    Not surprising on .c vs. .C

    And on embarrasing - been there, done that

    On this problem, I had to see it 2-3 times over the course of development before it stuck how to fix it (i.e., qmake, then make)...talk about feeling embarrassed

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Trying to get basic signals example working

    Quote Originally Posted by jpn View Post
    I'm not exactly sure if this has just been a problem in the past and already corrected.
    AFAIR Borland compiler didn't like any extension other than .cpp.

Similar Threads

  1. Mac OS X UI not working
    By hvengel in forum Qt Programming
    Replies: 3
    Last Post: 1st April 2006, 01:02
  2. Signals/Slots stopped working
    By Jimmy2775 in forum Qt Programming
    Replies: 8
    Last Post: 31st March 2006, 21:11
  3. Need Basic html Browser
    By awalesminfo in forum Newbie
    Replies: 6
    Last Post: 21st March 2006, 17:14
  4. Problem with Signals and Slots
    By Kapil in forum Newbie
    Replies: 11
    Last Post: 15th February 2006, 11:35
  5. KDE Signals
    By chombium in forum KDE Forum
    Replies: 1
    Last Post: 25th January 2006, 18:45

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.