[Solved] Header includes with static libraries depending on each other



  • Hello,

    I have some trouble figuring out how qmake handles the include dependencies of a static library that depends on another static library.

    Short version : I have 2 static libraries lib1 and lib2, with lib2 depending on lib1. An application app uses lib2. When compiling the app, should the include path (in the .pro file) only contains the lib2 headers or both lib1 and lib2 headers ? Intuitively, I would say that it should only include lib2 headers. But ... I got a compilation error saying that the headers of lib1 are not found... I find quite annoying and cumbersome to have to include both lib1 and lib2 headers, especially in case of multiple dependencies (i.e. more than 2 libraries involved). Is there a way to avoid this ?

    Long version :
    My folder structure is as below :
    @ --- src/
    lib1/
    src/ (.pro,.h,.cpp)
    lib2/
    src/ (.pro,.h,.cpp)
    app/
    src/ (.pro,.h,.cpp)
    --- build/
    lib1/
    lib1.a
    lib2/
    lib2.a
    app/
    app@

    lib1.pro
    @
    TARGET = lib1
    TEMPLATE = lib
    CONFIG += staticlib
    CONFIG += create_prl link_prl
    SOURCES += ...
    HEADERS += ...
    @

    lib2.pro
    @
    TARGET = lib2
    TEMPLATE = lib
    CONFIG += staticlib
    CONFIG += create_prl link_prl

    INCLUDEPATH += $${PRO_FILE_PWD}/../../lib1/src
    DEPENDPATH += $${PRO_FILE_PWD}/../../lib1/src
    PRE_TARGETDEPS += $${OUT_PWD}/../lib1/liblib1.a
    LIBS += -L$${OUT_PWD}/../lib1 -llib1

    SOURCES += ...
    HEADERS += ...
    @

    lib2.h
    @
    ...
    #include "header_lib1.h"
    ...
    @

    app.pro
    @
    TARGET = app
    TEMPLATE = app
    CONFIG += link_prl

    lib2

    INCLUDEPATH += $${PRO_FILE_PWD}/../../lib2/src
    DEPENDPATH +=$${PRO_FILE_PWD}/../../lib2/src
    PRE_TARGETDEPS += $${OUT_PWD}/../lib2/liblib2.a
    LIBS += -L$${OUT_PWD}/../lib2 -llib2

    SOURCES += ...
    HEADERS += ...--
    @

    When trying to compile the app, I get an error : header_lib1.h not found. I figured out 2 ways to solve this :

    In app.pro, add the following :
    @

    Include to lib1 headers

    INCLUDEPATH += $${PRO_FILE_PWD}/../../lib1/src
    DEPENDPATH += $${PRO_FILE_PWD}/../../lib1/src
    @

    Or change the include statement in lib2.h :
    @
    ...
    #include "../lib1/header_lib1.h"
    ...
    @

    But, I would like to avoid these since

    • the first solution requires to manually add the lib1 headers in the INCLUDEPATH of app.pro, which requires to keep track of the dependencies between the libraries. This is not convenient at all, especially when having multiple "cascade" dependencies. Is there a way to use lib2 in the app without explicitly referring to lib1 ?

    • the second solutions explicitly mentions the path of the lib1 headers, which I would like to avoid.

    Any suggestion is welcome =)


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    What you can do is a pri file per library that you include in you app.pro

    @
    lib1.pri:
    INCLUDEPATH += $${PWD}/src
    DEPENDPATH += $${PWD}/src

    Add libs defines etc.

    app.pro:
    include(../../lib1/lib1.pri)
    @

    Hope it helps



  • Thank you for your answer. Indeed, it is a bit more convenient to include a pri file. But actually, I would like to avoid to explicitly mentions the lib1 header includes in app.pro, since app depends directly on lib2, but only indirectly on lib1 (because lib2 depends on lib1). Not sure if this is possible or make sense ?


  • Lifetime Qt Champion

    If you don't have any headers from lib1 included in you lib2 headers then just include lib2.pri otherwise you'll need both



  • Alright, thank you for your answers !


  • Lifetime Qt Champion

    You're welcome !

    If this answers your question, please update the thread title prepending [solved] so other forum users may know a solution has been found :)

    Happy coding !


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.