QtCreator, subproject linker error

I have a fairly large application that I need to create / support, so I decided to use googletest and for convenience I wanted to structure the test and application code as subprojects. I created a super project with the following structure:

SuperProject - SuperProject.pro - defaults.pri - Application -- Application.pro -- Sources -- main.cpp -- Headers - Tests -- Tests.pro -- main.cpp -- Sources -- Headers 

From superproject.pro

 TEMPLATE = subdirs CONFIG += ordered SUBDIRS += \ Application \ Tests \ OTHER_FILES += \ defaults.pri 

With defaults.pri

 INCLUDEPATH += $$PWD/Application 

And Tests.pro

 include(gtest_dependency.pri) include(../defaults.pri) TEMPLATE = app QT += core CONFIG += console c++11 CONFIG -= app_bundle CONFIG += thread HEADERS += tst_redoundo.h SOURCES += main.cpp 

And Application.pro

 include(ExcelLib/qtxlsx.pri) include(../defaults.pri) TEMPLATE = app QT += qml quick CONFIG += c++14 static { # everything below takes effect with CONFIG ''= static CONFIG+= static CONFIG += staticlib # this is needed if you create a static library, not a static executable DEFINES+= STATIC message("~~~ static build ~~~") # this is for information, that the static build is done win32: TARGET = $$join(TARGET,,,s) #this adds an s in the end, so you can seperate static build from } RC_ICONS += msiconbmp.ico SOURCES += ..omitted RESOURCES += qml.qrc # Additional import path used to resolve QML modules in Qt Creator code model QML_IMPORT_PATH = # Additional import path used to resolve QML modules just for Qt Quick Designer QML_DESIGNER_IMPORT_PATH = # The following define makes your compiler emit warnings if you use # any feature of Qt which as been marked deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if you use deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target DISTFILES += *.pri HEADERS += ..omitted 

: w

The application compiles and works fine on its own, as does the test code. But as soon as I try to include something from the application, for example,

 #include "util.h" #include "tst_redoundo.h" #include <gtest/gtest.h> int main(int argc, char *argv[]) { Util u; ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } 

The code compiles, but does not bind to, an undefined reference to the Util constructor. Most tutorials with the same setup as mine assume that the pattern that the test code links are associated with is TEMPLATE = lib , but I can't change the pattern from app to lib for the application. How can I link the linker to the application?

+8
c ++ qt
source share
2 answers

Firstly, I do not see anywhere that you linked the gtest library in a test subproject. Add command LIBS += -lgtest

More generally, you have only two options. Either you compile your main project as lib, which then refers to your testing subproject, or you must include all your .cpp files in the testing. In any case, since this is a separate project, your testing project does not know your other (sub) projects.

I hope this points you to the correct answer.

+5
source share

I came to a decision by doing what David Luke suggested below, his answer is incomplete, but he will still have generosity from me.

As a result, I included each .cpp file in Tests.pro sources, for example, "../Application/util.cpp". This means that I essentially create the application twice, once, as an application, and again when, when compiling the Test project, the object files of the same .cpp are in two different projects. Not nice and time consuming, but it works.

I would like to avoid duplication of object files.

0
source share

All Articles