Results 1 to 2 of 2

Thread: QtTest: forced to #include .cpp file

  1. #1
    Join Date
    May 2009
    Location
    Canada
    Posts
    163
    Thanks
    7
    Thanked 20 Times in 20 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Windows Android

    Default QtTest: forced to #include .cpp file

    Hello,

    I just posted a different question about the same testing code here.

    My test suite includes a class called TestOutgoingCall, which tests a class called -- surprise surprise -- OutgoingCall. That class lives one directory up. As a convenience, therefore, I added .. to the INCLUDEPATH of my test .pro file:

    Qt Code:
    1. QT += core testlib
    2. TEMPLATE = app
    3. TARGET = test
    4. INCLUDEPATH += . ..
    5.  
    6. # Input
    7. SOURCES += \
    8. main.cpp \
    9. testoutgoingcall.cpp \
    10. testscopelock.cpp
    11.  
    12. HEADERS += \
    13. testoutgoingcall.h \
    14. testscopelock.h \
    15. testcase.h
    To copy to clipboard, switch view to plain text mode 

    Here is testoutgoingcall.h:

    Qt Code:
    1. #ifndef TESTOUTGOINGCALL_H
    2. #define TESTOUTGOINGCALL_H
    3.  
    4. #include "testcase.h"
    5.  
    6. class OutgoingCall;
    7.  
    8. class TestOutgoingCall : public TestCase
    9. {
    10. Q_OBJECT
    11. private slots:
    12. void initTestCase();
    13. void cleanupTestCase();
    14.  
    15. private:
    16. OutgoingCall *call;
    17. };
    18.  
    19. #endif // TESTOUTGOINGCALL_H
    To copy to clipboard, switch view to plain text mode 

    and here is the .cpp file:

    Qt Code:
    1. #include "testoutgoingcall.h"
    2.  
    3. #include "OutgoingCall.h"
    4. #include "OutgoingCall.cpp" // FIXME WHY DO WE NEED THIS TO COMPILE?! 07.31.15
    5.  
    6. void TestOutgoingCall::initTestCase() {
    7. call = new OutgoingCall("", "");
    8. }
    9.  
    10. void TestOutgoingCall::cleanupTestCase() {
    11. delete call;
    12. }
    To copy to clipboard, switch view to plain text mode 

    As you can see, the implementation is as yet trivial, which is fine. The problem is that if I remove #include "OutgoingCall.cpp" I get the following compiler errors:

    Qt Code:
    1. Undefined symbols for architecture x86_64:
    2. "OutgoingCall::OutgoingCall(QString, QString)", referenced from:
    3. TestOutgoingCall::initTestCase() in testoutgoingcall.o
    4. ld: symbol(s) not found for architecture x86_64
    5. clang: error: linker command failed with exit code 1 (use -v to see invocation)
    6. make: *** [test.app/Contents/MacOS/test] Error 1
    7. 15:43:14: The process "/usr/bin/make" exited with code 2.
    8. Error while building/deploying project test (kit: Desktop Qt 5.5.0 clang 64bit)
    9. When executing step "Make"
    To copy to clipboard, switch view to plain text mode 

    The constructor for Outgoing call takes two QStrings. Everything works when I un-comment that include.

    What is going on here? I'm fairly certain that including a .cpp file after the .h file is the Wrong Way To Do It.

    Any insight is appreciated.

    Thank you!

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QtTest: forced to #include .cpp file

    The test program needs to be complete, that is, it must have an implementation of OutgoingCall and not just a declaration. The linker cannot find the implementation, i.e. Undefined symbols, and fails to put together a complete program. Your PRO file does not pull that implementation in either as a SOURCES or linked as part of a library. Your #include kludge drags the source of the implementation directly into your test source code, making the implementation available.

    The cleaner way would be adding to SOURCES. If the OutgoingCall class is a QObject then you should also add the header to the HEADERS variable so that moc gets to see it.

    Qt Code:
    1. QT += core testlib
    2. TEMPLATE = app
    3. TARGET = test
    4. INCLUDEPATH += . ..
    5.  
    6. # Input
    7. SOURCES += \
    8. main.cpp \
    9. testoutgoingcall.cpp \
    10. testscopelock.cpp \
    11. ..\OutgoingCall.cpp
    12.  
    13. HEADERS += \
    14. testoutgoingcall.h \
    15. testscopelock.h \
    16. testcase.h \
    17. ..\OutgoingCall.h
    To copy to clipboard, switch view to plain text mode 

    The INCLUDEPATH is only used to find included files that are not literally where the #include says. The INCLUDEPATH ".." entry is necessary if you wish to use:
    Qt Code:
    1. #include "OutgoingCall.h"
    2. // instead of
    3. #include "../OutgoingCall.h"
    To copy to clipboard, switch view to plain text mode 
    in your test code.

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

    Urthas (5th August 2015)

Similar Threads

  1. Replies: 2
    Last Post: 21st March 2014, 18:53
  2. QWidget forced the application closed
    By cic in forum Newbie
    Replies: 0
    Last Post: 27th June 2013, 17:41
  3. Replies: 1
    Last Post: 23rd May 2011, 04:53
  4. Replies: 3
    Last Post: 1st November 2010, 16:33
  5. Replies: 4
    Last Post: 9th May 2010, 16:18

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.