gpt4 book ai didi

c++ - QtCreator,子项目链接器错误

转载 作者:可可西里 更新时间:2023-11-01 17:55:36 28 4
gpt4 key购买 nike

我有一个相当大的应用程序需要构建/维护,所以我决定使用 googletest为了方便起见,想将测试和应用程序代码构建为子项目。我创建了一个具有以下结构的 super 项目:

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

应用程序自行编译并运行良好,测试代码也是如此。但是一旦我尝试像这样从 Application 中包含任何内容

#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 构造函数的 undefined reference 。大多数与我的设置相同的指南都假定测试代码链接的模板是 TEMPLATE = lib 但我无法将模板从 app 更改为 lib 用于应用程序。如何让链接器链接到应用程序?

最佳答案

首先,我在任何地方都看不到您在测试子项目中链接了 gtest 库。要添加的命令是 LIBS += -lgtest

更一般地说,您只有两个选择。要么将主项目编译为 lib,然后链接到测试子项目,要么必须在测试中包含所有 .cpp 文件。无论如何,由于它是一个单独的项目,您的测试项目不知道您的其他(子)项目。

我希望这会为您指明正确的答案

关于c++ - QtCreator,子项目链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44306538/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com