C++ Qt包含来自不同项目的库
我有一个基本的Qt项目a 我想包含其他projectB,所以我将include$PATH_to_B_PROJECT/defaults.pri放入项目的.pro文件中 $PATH_TO_B_项目是硬编码的相对路径 如果我将项目B中的any.h包含到项目A中,则没有错误,但是如果我想使用包含库中的类,则会得到错误:对ClassToBeUsed的未定义引用 我怎样才能解决这个问题?谢谢 对ClassToBeUsed的未定义引用意味着链接器无法找到定义,即ClassToBeUsed方法的主体。解决方案取决于项目B的类型 如果是库,则需要如下链接:C++ Qt包含来自不同项目的库,c++,qt,C++,Qt,我有一个基本的Qt项目a 我想包含其他projectB,所以我将include$PATH_to_B_PROJECT/defaults.pri放入项目的.pro文件中 $PATH_TO_B_项目是硬编码的相对路径 如果我将项目B中的any.h包含到项目A中,则没有错误,但是如果我想使用包含库中的类,则会得到错误:对ClassToBeUsed的未定义引用 我怎样才能解决这个问题?谢谢 对ClassToBeUsed的未定义引用意味着链接器无法找到定义,即ClassToBeUsed方法的主体。解决方案取
LIBS += -lprojectB
#Network include dirs
INCLUDEPATH += $$PWD
DEPENDPATH += $$PWD
#include the debug and definitions
!include( $$PWD/../DebugAndDefinitions/includeDebugAndDefinitions.pri ){
error( "includeDirs not included. Filename: $$PWD/../DebugAndDefinitions/includeDebugAndDefinitions.pri" )
}
#include the database
!include( $$PWD/../Database/includeDatabase.pri ){
error( "includeDirs not included. Filename: $$PWD/../Database/includeDatabase.pri" )
}
#Include the libraries, if the project is not the pri's parent
!equals(_PRO_FILE_PWD_, $$PWD) {
CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../Network/release/ -lNetwork
CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../Network/debug/ -lNetwork
CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../Network/release/Network.lib
CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../Network/debug/Network.lib
}
install_config_network_core.path = $$OUT_PWD/config/
install_config_network_core.files = $$_PRO_FILE_PWD_/../Network/config/network.xml
install_config_network_core.extra = $(COPY_FILE) \"$$shell_path($$_PRO_FILE_PWD_/../Network/config/core.xml)\" \"$$OUT_PWD/config/network.xml\"
install_config_network_gui.path = $$OUT_PWD/config/
install_config_network_gui.files = $$_PRO_FILE_PWD_/../Network/config/network.xml
install_config_network_gui.extra = $(COPY_FILE) \"$$shell_path($$_PRO_FILE_PWD_/../Network/config/gui.xml)\" \"$$OUT_PWD/config/network.xml\"
QT -= gui
QT += network
TARGET = Network
TEMPLATE = lib
CONFIG += staticlib
SOURCES += \
NetworkConfigurator.cpp \
UdpHandler.cpp \
IOSSocket.cpp \
IOSServer.cpp \
IOSBeacon.cpp
HEADERS += \
NetworkConfigurator.h \
UdpHandler.h \
IOSSocket.h \
IOSServer.h \
IOSBeacon.h
RESOURCES += \
network.qrc
OTHER_FILES += \
includeNetwork.pri
#include the projects pri file
!include( $$PWD/includeNetwork.pri ){
error( "includeDirs not included. Filename: $$PWD/includeNetwork.pri" )
}
如果只是源文件,则需要在defaults.pri文件中添加cpp文件:
对ClassToBeUsed的未定义引用意味着链接器无法找到定义,即ClassToBeUsed方法的主体。解决方案取决于项目B的类型
如果是库,则需要如下链接:
LIBS += -lprojectB
#Network include dirs
INCLUDEPATH += $$PWD
DEPENDPATH += $$PWD
#include the debug and definitions
!include( $$PWD/../DebugAndDefinitions/includeDebugAndDefinitions.pri ){
error( "includeDirs not included. Filename: $$PWD/../DebugAndDefinitions/includeDebugAndDefinitions.pri" )
}
#include the database
!include( $$PWD/../Database/includeDatabase.pri ){
error( "includeDirs not included. Filename: $$PWD/../Database/includeDatabase.pri" )
}
#Include the libraries, if the project is not the pri's parent
!equals(_PRO_FILE_PWD_, $$PWD) {
CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../Network/release/ -lNetwork
CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../Network/debug/ -lNetwork
CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../Network/release/Network.lib
CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../Network/debug/Network.lib
}
install_config_network_core.path = $$OUT_PWD/config/
install_config_network_core.files = $$_PRO_FILE_PWD_/../Network/config/network.xml
install_config_network_core.extra = $(COPY_FILE) \"$$shell_path($$_PRO_FILE_PWD_/../Network/config/core.xml)\" \"$$OUT_PWD/config/network.xml\"
install_config_network_gui.path = $$OUT_PWD/config/
install_config_network_gui.files = $$_PRO_FILE_PWD_/../Network/config/network.xml
install_config_network_gui.extra = $(COPY_FILE) \"$$shell_path($$_PRO_FILE_PWD_/../Network/config/gui.xml)\" \"$$OUT_PWD/config/network.xml\"
QT -= gui
QT += network
TARGET = Network
TEMPLATE = lib
CONFIG += staticlib
SOURCES += \
NetworkConfigurator.cpp \
UdpHandler.cpp \
IOSSocket.cpp \
IOSServer.cpp \
IOSBeacon.cpp
HEADERS += \
NetworkConfigurator.h \
UdpHandler.h \
IOSSocket.h \
IOSServer.h \
IOSBeacon.h
RESOURCES += \
network.qrc
OTHER_FILES += \
includeNetwork.pri
#include the projects pri file
!include( $$PWD/includeNetwork.pri ){
error( "includeDirs not included. Filename: $$PWD/includeNetwork.pri" )
}
如果只是源文件,则需要在defaults.pri文件中添加cpp文件:
编辑:
我应该提一下。您收到此错误消息的原因是您的项目没有包含标题。但不包括包含实现的.lib文件。请注意,您需要为每个builte.g包含不同的.lib文件。调试、发布等
解决方案:
我们的团队花了很长时间才发现。我会给你我们的解决方案。我不知道这是官方的还是最好的解决方案。或者如果QT中存在这样的东西,考虑到它的本质
按照我们的实现方式,每个项目都有一个.pro和一个.pri文件。这是最简单的思考方式。将考虑.PRI文件、项目头和配置文件的项目实现。
因此,项目需要的任何东西,但项目的依赖项不需要。进入.pro文件。依赖项所需的任何内容都将放在.pri文件中。不用说,pro文件包括它自己的pri文件
现在,对于我们来说,一个简单的.pri文件includeNetwork.pri如下所示:
LIBS += -lprojectB
#Network include dirs
INCLUDEPATH += $$PWD
DEPENDPATH += $$PWD
#include the debug and definitions
!include( $$PWD/../DebugAndDefinitions/includeDebugAndDefinitions.pri ){
error( "includeDirs not included. Filename: $$PWD/../DebugAndDefinitions/includeDebugAndDefinitions.pri" )
}
#include the database
!include( $$PWD/../Database/includeDatabase.pri ){
error( "includeDirs not included. Filename: $$PWD/../Database/includeDatabase.pri" )
}
#Include the libraries, if the project is not the pri's parent
!equals(_PRO_FILE_PWD_, $$PWD) {
CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../Network/release/ -lNetwork
CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../Network/debug/ -lNetwork
CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../Network/release/Network.lib
CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../Network/debug/Network.lib
}
install_config_network_core.path = $$OUT_PWD/config/
install_config_network_core.files = $$_PRO_FILE_PWD_/../Network/config/network.xml
install_config_network_core.extra = $(COPY_FILE) \"$$shell_path($$_PRO_FILE_PWD_/../Network/config/core.xml)\" \"$$OUT_PWD/config/network.xml\"
install_config_network_gui.path = $$OUT_PWD/config/
install_config_network_gui.files = $$_PRO_FILE_PWD_/../Network/config/network.xml
install_config_network_gui.extra = $(COPY_FILE) \"$$shell_path($$_PRO_FILE_PWD_/../Network/config/gui.xml)\" \"$$OUT_PWD/config/network.xml\"
QT -= gui
QT += network
TARGET = Network
TEMPLATE = lib
CONFIG += staticlib
SOURCES += \
NetworkConfigurator.cpp \
UdpHandler.cpp \
IOSSocket.cpp \
IOSServer.cpp \
IOSBeacon.cpp
HEADERS += \
NetworkConfigurator.h \
UdpHandler.h \
IOSSocket.h \
IOSServer.h \
IOSBeacon.h
RESOURCES += \
network.qrc
OTHER_FILES += \
includeNetwork.pri
#include the projects pri file
!include( $$PWD/includeNetwork.pri ){
error( "includeDirs not included. Filename: $$PWD/includeNetwork.pri" )
}
它的.pro fileread实现文件如下所示:
LIBS += -lprojectB
#Network include dirs
INCLUDEPATH += $$PWD
DEPENDPATH += $$PWD
#include the debug and definitions
!include( $$PWD/../DebugAndDefinitions/includeDebugAndDefinitions.pri ){
error( "includeDirs not included. Filename: $$PWD/../DebugAndDefinitions/includeDebugAndDefinitions.pri" )
}
#include the database
!include( $$PWD/../Database/includeDatabase.pri ){
error( "includeDirs not included. Filename: $$PWD/../Database/includeDatabase.pri" )
}
#Include the libraries, if the project is not the pri's parent
!equals(_PRO_FILE_PWD_, $$PWD) {
CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../Network/release/ -lNetwork
CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../Network/debug/ -lNetwork
CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../Network/release/Network.lib
CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../Network/debug/Network.lib
}
install_config_network_core.path = $$OUT_PWD/config/
install_config_network_core.files = $$_PRO_FILE_PWD_/../Network/config/network.xml
install_config_network_core.extra = $(COPY_FILE) \"$$shell_path($$_PRO_FILE_PWD_/../Network/config/core.xml)\" \"$$OUT_PWD/config/network.xml\"
install_config_network_gui.path = $$OUT_PWD/config/
install_config_network_gui.files = $$_PRO_FILE_PWD_/../Network/config/network.xml
install_config_network_gui.extra = $(COPY_FILE) \"$$shell_path($$_PRO_FILE_PWD_/../Network/config/gui.xml)\" \"$$OUT_PWD/config/network.xml\"
QT -= gui
QT += network
TARGET = Network
TEMPLATE = lib
CONFIG += staticlib
SOURCES += \
NetworkConfigurator.cpp \
UdpHandler.cpp \
IOSSocket.cpp \
IOSServer.cpp \
IOSBeacon.cpp
HEADERS += \
NetworkConfigurator.h \
UdpHandler.h \
IOSSocket.h \
IOSServer.h \
IOSBeacon.h
RESOURCES += \
network.qrc
OTHER_FILES += \
includeNetwork.pri
#include the projects pri file
!include( $$PWD/includeNetwork.pri ){
error( "includeDirs not included. Filename: $$PWD/includeNetwork.pri" )
}
有几件事值得一提
我们有时在检测哪个项目是“脏的”并且需要编译时会遇到问题。导致构建不完整或过度完成。我不知道这个问题该归咎于什么
这是专为windows编写的。如果您需要Linux或多平台实现方面的帮助。只要喊一声:
这是为静态库编写的。使用DLL可能需要稍微不同的方法
同样,这只是我们的方法。如果您认为有什么事情可以做得不同或更好,我很高兴听到:
编辑:
我应该提一下。您收到此错误消息的原因是您的项目没有包含标题。但不包括包含实现的.lib文件。请注意,您需要为每个builte.g包含不同的.lib文件。调试、发布等
解决方案:
我们的团队花了很长时间才发现。我会给你我们的解决方案。我不知道这是官方的还是最好的解决方案。或者如果QT中存在这样的东西,考虑到它的本质
按照我们的实现方式,每个项目都有一个.pro和一个.pri文件。这是最简单的思考方式。将考虑.PRI文件、项目头和配置文件的项目实现。
因此,项目需要的任何东西,但项目的依赖项不需要。进入.pro文件。依赖项所需的任何内容都将放在.pri文件中。不用说,pro文件包括它自己的pri文件
现在,对于我们来说,一个简单的.pri文件includeNetwork.pri如下所示:
LIBS += -lprojectB
#Network include dirs
INCLUDEPATH += $$PWD
DEPENDPATH += $$PWD
#include the debug and definitions
!include( $$PWD/../DebugAndDefinitions/includeDebugAndDefinitions.pri ){
error( "includeDirs not included. Filename: $$PWD/../DebugAndDefinitions/includeDebugAndDefinitions.pri" )
}
#include the database
!include( $$PWD/../Database/includeDatabase.pri ){
error( "includeDirs not included. Filename: $$PWD/../Database/includeDatabase.pri" )
}
#Include the libraries, if the project is not the pri's parent
!equals(_PRO_FILE_PWD_, $$PWD) {
CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../Network/release/ -lNetwork
CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../Network/debug/ -lNetwork
CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../Network/release/Network.lib
CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../Network/debug/Network.lib
}
install_config_network_core.path = $$OUT_PWD/config/
install_config_network_core.files = $$_PRO_FILE_PWD_/../Network/config/network.xml
install_config_network_core.extra = $(COPY_FILE) \"$$shell_path($$_PRO_FILE_PWD_/../Network/config/core.xml)\" \"$$OUT_PWD/config/network.xml\"
install_config_network_gui.path = $$OUT_PWD/config/
install_config_network_gui.files = $$_PRO_FILE_PWD_/../Network/config/network.xml
install_config_network_gui.extra = $(COPY_FILE) \"$$shell_path($$_PRO_FILE_PWD_/../Network/config/gui.xml)\" \"$$OUT_PWD/config/network.xml\"
QT -= gui
QT += network
TARGET = Network
TEMPLATE = lib
CONFIG += staticlib
SOURCES += \
NetworkConfigurator.cpp \
UdpHandler.cpp \
IOSSocket.cpp \
IOSServer.cpp \
IOSBeacon.cpp
HEADERS += \
NetworkConfigurator.h \
UdpHandler.h \
IOSSocket.h \
IOSServer.h \
IOSBeacon.h
RESOURCES += \
network.qrc
OTHER_FILES += \
includeNetwork.pri
#include the projects pri file
!include( $$PWD/includeNetwork.pri ){
error( "includeDirs not included. Filename: $$PWD/includeNetwork.pri" )
}
它的.pro fileread实现文件如下所示:
LIBS += -lprojectB
#Network include dirs
INCLUDEPATH += $$PWD
DEPENDPATH += $$PWD
#include the debug and definitions
!include( $$PWD/../DebugAndDefinitions/includeDebugAndDefinitions.pri ){
error( "includeDirs not included. Filename: $$PWD/../DebugAndDefinitions/includeDebugAndDefinitions.pri" )
}
#include the database
!include( $$PWD/../Database/includeDatabase.pri ){
error( "includeDirs not included. Filename: $$PWD/../Database/includeDatabase.pri" )
}
#Include the libraries, if the project is not the pri's parent
!equals(_PRO_FILE_PWD_, $$PWD) {
CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../Network/release/ -lNetwork
CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../Network/debug/ -lNetwork
CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../Network/release/Network.lib
CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../Network/debug/Network.lib
}
install_config_network_core.path = $$OUT_PWD/config/
install_config_network_core.files = $$_PRO_FILE_PWD_/../Network/config/network.xml
install_config_network_core.extra = $(COPY_FILE) \"$$shell_path($$_PRO_FILE_PWD_/../Network/config/core.xml)\" \"$$OUT_PWD/config/network.xml\"
install_config_network_gui.path = $$OUT_PWD/config/
install_config_network_gui.files = $$_PRO_FILE_PWD_/../Network/config/network.xml
install_config_network_gui.extra = $(COPY_FILE) \"$$shell_path($$_PRO_FILE_PWD_/../Network/config/gui.xml)\" \"$$OUT_PWD/config/network.xml\"
QT -= gui
QT += network
TARGET = Network
TEMPLATE = lib
CONFIG += staticlib
SOURCES += \
NetworkConfigurator.cpp \
UdpHandler.cpp \
IOSSocket.cpp \
IOSServer.cpp \
IOSBeacon.cpp
HEADERS += \
NetworkConfigurator.h \
UdpHandler.h \
IOSSocket.h \
IOSServer.h \
IOSBeacon.h
RESOURCES += \
network.qrc
OTHER_FILES += \
includeNetwork.pri
#include the projects pri file
!include( $$PWD/includeNetwork.pri ){
error( "includeDirs not included. Filename: $$PWD/includeNetwork.pri" )
}
有几件事值得一提
我们有时在检测哪个项目是“脏的”并且需要编译时会遇到问题。导致构建不完整或过度完成。我不知道这个问题该归咎于什么
这是专为windows编写的。如果您需要Linux或多平台实现方面的帮助。只要喊一声:
这是为静态库编写的。使用DLL可能需要稍微不同的方法
同样,这只是我们的方法。如果您认为有什么事情可以做得不同或更好,我很高兴听到:
事实证明,这要容易得多。只需打开这两个项目,而项目A是一个活动项目,请转到项目->依赖项->选择B项目。事实证明,这要容易得多。只要打开这两个项目,而项目A是一个活动项目,转到项目->依赖项->选择B项目。如果我将源添加到default.pri并尝试编译B项目,我没有规则将目标文件设为.cpp',需要文件.o'。停下。@cosbelix那是因为你是公司的
在不同的项目中删除源文件。这通常不是它的工作方式。请尝试包含lib文件。如果我将源代码添加到default.pri,并尝试编译B项目,则没有规则生成file.o所需的目标文件.cpp。停止。@cosbelix这是因为您在另一个项目中包含了一个源文件。这通常不是它的工作方式。请尝试包含lib文件。此.pri文件位于包含的项目中或包含其他文件的项目中?@cosbelix需要此项目依赖项的任何项目中都包含.pri文件。所以,如果数据库需要网络,它就包括它。它在它的.pri文件中实现了这一点。这样,如果某个对象使用数据库,它也将使用network.this.pri文件位于包含另一个对象的项目或包含另一个对象的项目中?@cosbelix.pri文件包含在任何需要此项目依赖项的项目中。所以,如果数据库需要网络,它就包括它。它在它的.pri文件中实现了这一点。这样,若有东西使用数据库,它也会使用网络。@cosbelix只是为了澄清,这实际上会做一个版本的建议答案。也就是说,它只会在你的.pro或.pri文件中添加一些行。@cosbelix只是为了澄清,这实际上会生成建议答案的一个版本。也就是说,它只会在.pro或.pri文件中添加一些行。