我有一个相当大的应用程序,我需要构建/维护所以我决定使用googletest,为方便起见,想要将测试和应用程序代码构建为子项目.我创建了一个具有以下结构的超级项目:
SuperProject
- SuperProject.pro
- defaults.pri
- Application
-- Application.pro
-- Sources
-- main.cpp
-- Headers
- Tests
-- Tests.pro
-- main.cpp
-- Sources
-- Headers
Run Code Online (Sandbox Code Playgroud)
同 superproject.pro
TEMPLATE = subdirs
CONFIG += ordered
SUBDIRS += \
Application \
Tests \
OTHER_FILES += \
defaults.pri
Run Code Online (Sandbox Code Playgroud)
同 defaults.pri
INCLUDEPATH += $$PWD/Application
Run Code Online (Sandbox Code Playgroud)
和 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
Run Code Online (Sandbox Code Playgroud)
和 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
Run Code Online (Sandbox Code Playgroud)
: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();
}
Run Code Online (Sandbox Code Playgroud)
代码编译但不会链接到未定义的Util构造函数引用.大多数与我的设置相同的指南假设测试代码链接TEMPLATE = lib的模板是,但我无法将模板从app更改lib为应用程序.如何让链接器链接到Application?
小智 5
首先,我无法看到您在测试子项目中链接gtest库的任何地方.要添加的命令是LIBS += -lgtest
更一般地说,您只有两个选择.您可以将主项目编译为lib,然后链接到测试子项目,或者必须在测试中包含所有.cpp文件.在任何情况下,由于它是一个单独的项目,您的测试项目不了解您的其他(子)项目.
我希望这会指出你正确的答案