Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Nested subdir dependencies on top-level libs



  • I have a project organized like so:

    src/
      cmdline/
        cmdline_app1/
        cmdline_app2/
      app1/
      lib1/
      lib2/
    

    I have a subdirs template defined at the src/ level, which defines the cmdlnie, app1, lib1, lib2. cmdline is another subdirs template, which defines cmdline_app1 and cmdline_app2.

    The problem I've run into is defining dependencies on top-level projects from within the nested subdir projects. For example, how do I define a dependency on lib1 from cmdline_app1? subdir projects are unaware of projects higher up in the hierarchy, aka cmdline subdir is unaware of lib1 project, so I can't just define cmdline_app1.depends = lib2 in the cmdline.pro file.



  • @btse You need to add this in your .pro files in the subdirs:

    PRE_TARGETDEPS += \
            $$PWD/../../lib1/libyourlib1.a \
            $$PWD/../../lib2/libyourlib2.a \
    


  • @Infinity I have this already for all of my projects, in addition to DEPENDPATH. However, this only works when those dependencies have already been built at least once, aka the lib file exists. It won't build the project/lib if it hasn't been built yet.



  • @btse Can you post your src.pro file?



  • @Infinity

    src.pro

    TEMPLATE = subdirs
    SUBDIRS = \
    cmdline \
    app1 \
    lib1 \
    lib2
    
    app1.depends = lib1
    

    cmdline.pro

    TEMPLATE = subdirs
    SUBDIRS = \
    cmdline_app1 \
    cmdline_app2
    
    # Defining dependencies on lib1 or lib2 in this file is not possible
    


  • I'm rushing around at work right now, so I do promise to come back and try to help more empathetically later today.

    Meanwhile, as a quick hint, I have gotten my project to do something very close (possibly identical) to what you need, by following this: http://archive.is/https://www.toptal.com/qt/vital-guide-qmake

    My result ended up looking thusly: https://github.com/219-design/qt-qml-project-template-with-ci/blob/d9e4359cf1/main_gui.pro

    I'll come back in a bit when I have time to hopefully pinpoint exactly what is going wrong in your specific case. Meanwhile, HTH!



  • @KH-219Design Thanks for those, but I still don't think those capture my scenario. They detail flat structures, where all subdirs at the top level are just projects (apps or libs), and not more subdirs.

    One possible workaround is defining all projects at the top level (src.pro) explicitly, but this has a number of drawbacks, one of which it doesn't play nicely with Qt Creator. (nested subdirs are no longer shown in a tree hierarchy but are all displayed at the top level).

    Example:

    src.pro

    SUBDIRS = cmdline/cmdline_app1
    
    cmdline/cmdline_app1.depends = lib1 lib2
    


  • Oh. Now I see. "Subdirs within subdirs." Interesting problem. (Sorry I did not read carefully enough originally.) I have a project I can use locally to poke around at this concept. I'll see if I come up with anything tomorrow.



  • I've been doing some experimenting, with no "good news".

    Meanwhile, in the throes of tweaking all my pro files and getting nowhere, it occurred to me to examine the pro files inside the source code distribution of Qt itself.

    The results are not encouraging. I found the following files using SUBDIRS and depends, and I peeked into quite a good sampling of them and none of them seem to use depends with something from a "higher" (outer, enclosing) directory. I thought if I could find even just one such example inside Qt itself, then that would unlock the mystery of how to properly do it. (I still think that! If there is such a single working example, then it will prove this is doable.)

    qtimageformats/src/src.pro:plugins.depends = imageformats
    qtquickcontrols2/src/src.pro:quickcontrols2.depends = quicktemplates2
    qtmultimedia/src/src.pro:src_qtmmwidgets.depends = multimedia
    qtbase/src/src.pro:src_tools_moc.depends = src_tools_bootstrap
    qtbase/src/platformsupport/platformsupport.pro:    input.depends += devicediscovery
    qttools/src/designer/src/src.pro:    lib.depends = uiplugin
    qttools/src/src.pro:            linguist.depends = designer
    qttools/src/assistant/assistant.pro:assistant.depends = help
    qtdeclarative/src/plugins/qmltooling/qmltooling.pro:qmldbg_native.depends = packetprotocol
    

    ^^ some of the places I turned to, hoping to find inspiration. None found :(

    All it would take to end the chase is one bit of documentation stating: "targets given as arguments to depends must be targets declared in the same pro file" .... or if that is not true, then an example of how to do it otherwise...

    Frustrating that I cannot find such explicit documentation. Can anyone?



  • @KH-219Design I think using the

    .depends 
    .command
    .target
    

    mechanism is a possibility (https://doc.qt.io/qt-5/qmake-advanced-usage.html#adding-custom-targets). I'll have to do some testing though.

    I guess the other solution would be to change the structure of my codebase to a more flat structure, but it's quite a large codebase so it'd take a bit of effort, and would kind of defeat the purpose of having subdirs.


Log in to reply