Important: Please read the Qt Code of Conduct -

Getting subdirs project to work: subproject can't find static library binaries of other project

  • Hi all,
    I've stumbled upon a rather unexpected issue with qmake and project dependencies.
    Let's assume we have a static library and an application which depends on the static library. Naturally I'd want the build system to recompile the library if something has changed in its sources, before going on to building the application.

    So this is what commonly is done with the subdirs project type, which in my case is placed next to the app project file. I thus have the following structure:

    ├── theApp/
    │   ├──
    │   ├──  //<- the subdirs project
    │   └── (source files...)
    └── theLibrary/
        ├── abclibrary.pri
        └── (source files...)

    The Library
    The is nothing special, it looks something like this:

    QT += ...
    TEMPLATE = lib
    CONFIG += qt staticlib
    TARGET = abclibrary
    HEADERS += ...
    SOURCES += ...

    Note that I'm not explicitly defining the destination of compilaton outputs, like DESTDIR, MOC_DIR, OBJECTS_DIR, etc. because I want QtCreator's shadowbuild feature to place them wherever it points to. Typically this will be a directory above the source directory, and named according to the build type, platform, target name etc. to prevent clashes of previously existing builds. So in this example, the shadowbuild will happen in BigProject/build-abclibrary-staticlib-Debug/.

    Building the library through directly works as expected.

    The Application
    The is also nothing too peculiar:

    QT += ...
    TEMPLATE = app
    TARGET = abcapp
    HEADERS += ...
    SOURCES += ...

    Notice how I'm not hardcoding here how to link to abclibrary (e.g. by hardcoding LIBS += -L../theLibrary -labclibrary) because I don't know where the binaries will actually end up when building the library. So this is deferred to the .pri file in the library directory which looks like this:

    BASEDIR   = $${PWD}
    LIBS += -L$$shadowed($$PWD) -labclibrary

    And finally here's the subdirs project

    TEMPLATE = subdirs
    SUBDIRS = abclibrary abcapp
    abclibrary.file = ../theLibrary/
    abcapp.file  = ./
    abcapp.depends = abclibrary

    Here's the issue: It's not working™.
    In the final linking stage, I can see that the app is being built with the parameter "-L -labclibrary", so it seems the string $$shadowed($$PWD) is empty, and doesn't contain the true directory where the abclibrary binaries are placed. Now I've derived this structure from various tutorials on the internet, as well as the official documentation regarding the subdirs template, and I didn't expect something so bread-and-butter in terms of build management to be so hard with qmake. I've also tried using $$DESTDIR, which turned out to also be an empty string in the decisive build moment.

    How do you guys do it? Where is my mistake?

  • Lifetime Qt Champion


    IIRC, I used a combo of DESTDIR and $$OUT_PWD to put all libraries in a common folder so then it's easier to link the application to them.

Log in to reply