简体   繁体   中英

Qt include library from different project

I'm having a basic Qt project (A).

I wanted to include other project(B) so I put include ($PATH_TO_B_PROJECT/defaults.pri) into the .pro file of the A project.

$PATH_TO_B_PROJECT is hardcoded relative path.

If I include any .h from project B into project A there are no errors, however if I want to use the class from the included library, I get error: undefined reference to ClassToBeUsed

How can I fix that? thanks!

undefined reference to ClassToBeUsed means the linker cannot find the defininition (ie the body) of ClassToBeUsed 's methods. Solution depends on type of your project B.

If it's library, you need to link against it like this:

LIBS += -lprojectB

If it's just sources, then you need to add cpp files in your defaults.pri file:

SOURCES += $$PWD/path_from_pri_to_cpp/ClassToBeUsed.cpp

Edit:

I should have mentioned. The reason you get this error message, is your project does include the header. But does not include the .lib file containing the implementation. Note that you'll want to include a different .lib file for each built(eg debug, release, etc)

Solution:

Well, this took our team a long time to find out. I'll give you our solution. I don't know if this is the official, or best solution. Or if such a thing even exists in QT, given it's nature.

The way we've implemented it, every project has a .pro and a .pri file. The easiest way of thinking of these. Is to consider the .pri file the project "header" and the profile the projects implementation.

So anything a project needs but the project's dependencies don't need. Goes in the .pro file. Anything that is also required by the dependencies goes in the .pri file. It goes without saying that a pro file includes it's own pri file.

Now a simple .pri file (includeNetwork.pri) for us looks like this:

#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\"

And it's .pro file(read implementation file) looks like this:

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" )
}

A couple of things are worth mentioning

  • We sometimes have problems with detecting which project are 'dirty' and need compiling. Resulting in incomplete, or overcomplete builds. I don't know what's to blame for this problem.

  • This is written only for windows. If you need help with a Linux or multi-platform implementation. Just give a shout:)

  • This is written for a static library. Working with a DLL will probably require a slightly different approach

  • Again, this is just our approach. If there's anything you think could be done different or better, I'd be glad to hear it:)

It proved to be much easier. Just open the both project, while project A is an active project go to Projects->Dependencies->select the B project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM