0

I need to resolve a simple issue with a test project. However despite my attempts to look at stack overflow I cannot resolve it. The toy project has one main CMakeLists and two sub-folders. One mathlib and one is a unittest.

CMakeLists.txt
├───QTMathLib
└───QTUnitTest

the sources:

testmathlib.cpp

#include "testmathlib.h"
#include <QDebug>

TestMathLib::TestMathLib()
{
    qDebug() << __FILE__ <<" Created!";
}

int32_t TestMathLib::Add(int32_t a, int32_t b)
{
    return a+b;
}

tst_testadd.cpp

void TestAdd::initTestCase()
{
    TestMathLib testMathLib;
    int32_t result = testMathLib.Add(10, 20);

    qDebug() << "Result =" << result ;
    QVERIFY(30 == result);
}

However I want the UnitTest project to include the mathlib sources as below:

Add library to Cmake project

I tried this:

set (sources2
  ../QTMathLib/testmathlib.cpp
)
add_executable(TestAdd
    tst_testadd.cpp
)
add_library( mylib ${sources2} )
add_test(NAME TestAdd COMMAND TestAdd)
target_link_libraries(TestAdd PRIVATE Qt${QT_VERSION_MAJOR}::Gui
    Qt${QT_VERSION_MAJOR}::Test
    Qt${QT_VERSION_MAJOR}::Core
    mylib)

But this always give me a building error since I cannot resolve QDebug.h header file. Notice that other includes may be added, so don't just patch for the QDebug.h, assume any QTCore header can be included in the future.

D:\gmmo\qt_qml\QTTest\QTMathLib\testmathlib.cpp:2: error: QDebug: No such file or directory
D:/gmmo/qt_qml/QTTest/QTMathLib/testmathlib.cpp:2:10: fatal error: QDebug: No such file or directory
    2 | #include <QDebug>
      |          ^~~~~~~~

The fix should be on these files, not the source file itself.

└───QTUnitTest\CMakeLists.txt
├───QTMainApp\CMakeLists.txt

Any clues why it cannot file QT headers?

gmmo
  • 2,577
  • 3
  • 30
  • 56
  • 1
    Should not it be `target_link_libraries(mylib` but you apply `target_link_libraries(TestAdd`? – 273K Oct 06 '22 at 19:35
  • no, this was automatically generated by QT, so to build the unit test the QT creator generated this template code: target_link_libraries(TestAdd PRIVATE Qt${QT_VERSION_MAJOR}::Gui Qt${QT_VERSION_MAJOR}::Test) I just added the mylib and the QTCode dependencies. – gmmo Oct 06 '22 at 21:00
  • I guess don't understand exactly what you are suggesting me to try. If I remove mylib like so target_link_libraries(TestAdd PRIVATE Qt${QT_VERSION_MAJOR}::Gui Qt${QT_VERSION_MAJOR}::Test Qt${QT_VERSION_MAJOR}::Core) I get :-1: error: QTUnitTest/tst_testadd.cpp:37: undefined reference to `TestMathLib::Add(int, int)' What do want me to try exactly? – gmmo Oct 06 '22 at 22:28
  • I did add the library. I greatly appreciate your time helping me out how the target_link_libraries should look like. Here I added both Core and my lib: target_link_libraries(TestAdd PRIVATE Qt${QT_VERSION_MAJOR}::Core mylib Qt${QT_VERSION_MAJOR}::Gui Qt${QT_VERSION_MAJOR}::Test) I believe my problem is really here: set (sources2 ../QTMathLib/testmathlib.cpp ) since it complain it can't find QDebug.h but I don't get it is visible via the find_package. Well, would you be kind to paste the exact line you have in mind? thank you again! – gmmo Oct 06 '22 at 23:39
  • Just copy the entire `target_link_libraries(TestAdd ...` and change `TestAdd` to `mylib` in the copy. – 273K Oct 06 '22 at 23:43
  • you meant two targer_link_libraries correct? That worked. thx! I will post the final answer below. Thank you again! – gmmo Oct 07 '22 at 00:02

1 Answers1

0

thank you #273K for the help, this worked:

target_link_libraries(mylib PRIVATE
    Qt${QT_VERSION_MAJOR}::Core)


target_link_libraries(TestAdd PRIVATE
    mylib
    Qt${QT_VERSION_MAJOR}::Gui
    Qt${QT_VERSION_MAJOR}::Test)

Adding two target_link_libraries.

gmmo
  • 2,577
  • 3
  • 30
  • 56