我有一个相当大的应用程序,我需要构建/维护所以我决定使用googletest,为方便起见,希望将测试和应用程序代码构建为子项目。我创建了一个具有以下结构的超级项目:
SuperProject
- SuperProject.pro
- defaults.pri
- Application
-- Application.pro
-- Sources
-- main.cpp
-- Headers
- Tests
-- Tests.pro
-- main.cpp
-- Sources
-- Headers
superproject.pro
TEMPLATE = subdirs
CONFIG += ordered
SUBDIRS += \
Application \
Tests \
OTHER_FILES += \
defaults.pri
defaults.pri
INCLUDEPATH += $$PWD/Application
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
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's 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
应用程序编译并运行良好,测试代码也是如此。但是,只要我尝试从应用程序中包含任何内容,就像这样
#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();
}
代码编译但未与未定义的Util构造函数引用链接。与我的设置相同的大多数指南都假设测试代码链接的模板是TEMPLATE = lib
,但我无法将模板从app
更改为lib
的应用程序。如何让链接器链接到Application?
答案 0 :(得分:5)
首先,我无法看到您在测试子项目中链接gtest库的任何地方。要添加的命令是LIBS += -lgtest
更一般地说,您只有两个选择。您可以将主项目编译为lib,然后链接到测试子项目,或者必须在测试中包含所有.cpp
文件。无论如何,由于它是一个单独的项目,因此您的测试项目不了解您的其他(子)项目。
我希望这会指出你正确的答案
答案 1 :(得分:0)
我找到了解决David LUC建议的解决方案,他的回答是不完整的,但他仍然会得到我的赏金。
我最终做的是在Tests.pro的源代码中包含每个.cpp文件,例如“../Application/util.cpp”。这意味着我基本上构建应用程序两次,一次作为应用程序,再次编译Test项目时,同一.cpp的目标文件在两个不同的项目中。不漂亮,耗时,但它确实有效。
仍然希望避免重复的目标文件。