# Dependency Cycle with Qt6 / QML

**URL:** https://discourse.cmake.org/t/dependency-cycle-with-qt6-qml/7099
**Category:** Code
**Created:** [December 20, 2022, 5:00pm UTC](https://discourse.cmake.org/t/dependency-cycle-with-qt6-qml/7099 "2022-12-20T17:00:30Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![Leon0402](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/l/ccd318/32.png) [@Leon0402](https://discourse.cmake.org/u/Leon0402)
#### Post date: [December 20, 2022, 5:00pm UTC](https://discourse.cmake.org/t/dependency-cycle-with-qt6-qml/7099/1 "2022-12-20T17:00:30Z")

</div>

Hi,

I have some project, where I introduced some qml. The command is the following:

```auto
qt_add_qml_module(BeansClient
    URI Beans.Gui
    VERSION 1.0
    QML_FILES
        qml/Windows/Home/main.qml
        qml/Components/NavigationBar.qml
    RESOURCES
        qml/resources/images/logo.png
)

```

and it works fine. I then wanted to move this code in the qml folder and did a

```auto
add_subdirectory(qml) 

```

and

```auto
qt_add_qml_module(BeansClient
    URI Beans.Gui
    VERSION 1.0
    QML_FILES
        Windows/Home/main.qml
        Components/NavigationBar.qml
    RESOURCES
        resources/images/logo.png
)

```

But this lead to the following error message:

```auto
ninja: error: dependency cycle: src/BeansClient_autogen/timestamp -> src/qml/BeansClient_tooling -> src/qml/.rcc/qmlcache/BeansClient_Windows/Home/main_qml.cpp -> src/qml/Beans/Gui/BeansClient.qmltypes -> src/meta_types/qt6beansclient_debug_metatypes.json -> src/meta_types/BeansClient_json_file_list.txt -> src/BeansClient_autogen/timestamp

```

I saw a similar topic about this here [https://gitlab.kitware.com/cmake/cmake/-/issues/21977](https://gitlab.kitware.com/cmake/cmake/-/issues/21977). But either this a different issue or the one here is not solved.

Any help is appreciated.

Edit: With make there is also an error message, which might be more helpful?

```auto
CMake Error: The inter-target dependency graph contains the following strongly connected component (cycle):
  "BeansClient_autogen" of type UTILITY
    depends on "BeansClient_tooling" (strong)
  "BeansClient_automoc_json_extraction" of type UTILITY
    depends on "BeansClient_autogen" (strong)
  "BeansClient_tooling" of type INTERFACE_LIBRARY
    depends on "BeansClient_automoc_json_extraction" (strong)
At least one of these targets is not a STATIC_LIBRARY. Cyclic dependencies are allowed only among static libraries.
CMake Generate step failed. Build files cannot be regenerated correctly.

```

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [December 20, 2022, 9:48pm UTC](https://discourse.cmake.org/t/dependency-cycle-with-qt6-qml/7099/2 "2022-12-20T21:48:54Z")

</div>

I’ve been out of the loop on that code for a while, but @alcroito @jobor might have some thoughts on this one.

---

<div class="post-metadata">

### Author: ![jobor](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jobor/32/1230_2.png) [@jobor](https://discourse.cmake.org/u/jobor)
#### Post date: [December 21, 2022, 7:50am UTC](https://discourse.cmake.org/t/dependency-cycle-with-qt6-qml/7099/3 "2022-12-21T07:50:25Z")

</div>

#21977 looks unrelated.

It’s not entirely clear how exactly your project setup looks like.  
Maybe you could post a small example that exhibits the issue.  
But your issue seems similar to [Loading...](https://bugreports.qt.io/browse/QTBUG-99768)

Quote from the comments:

> You should keep the QML files in the same directory as the CMakeLists.txt that declares the module. Otherwise you’re in for a world of pain.

---

<div class="post-metadata">

### Author: ![Leon0402](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/l/ccd318/32.png) [@Leon0402](https://discourse.cmake.org/u/Leon0402)
#### Post date: [December 21, 2022, 9:08am UTC](https://discourse.cmake.org/t/dependency-cycle-with-qt6-qml/7099/4 "2022-12-21T09:08:23Z")

</div>

Sure! I thought it would be difficult to reproduce, but in fact it’s very easy.

[qm\_cycle.zip](https://discourse.cmake.org/uploads/short-url/f5AugyFaR7g6piX6VvK4rXrdS7d.zip) (4.4 KB)

Here is a small sample project. Currently it doesn’t work. If you remove in `src/CMakeLists.txt` the add\_subdirectory and use the qml module command there, it should work.

So I see two potential issues:

- I don’t have the qml files in the same directory =\> World of pain? (Note: I also don’t have them in the same directory with the working solution)
- I don’t have my module definition in the same file as my backing executable target =\> [https://bugreports.qt.io/browse/QTBUG-95200](https://bugreports.qt.io/browse/QTBUG-95200)? =\> [https://cmake.org/cmake/help/v3.21/policy/CMP0118.html](https://cmake.org/cmake/help/v3.21/policy/CMP0118.html) =\> I set cmake 3.21 as the minimum version and also tried turning it on explicitly. That didn’t seem to fix it.

So the comments there state that you should always put the qml files in the same directory for reasons.

Question 1: Why doesn’t the docs mention it?  
Question 2: Why is there no explicit warning about this  
Question 3: Most importantly: How should this work in any real project? This is crazy!! Putting everything in one folder, what a mess. And the only solution is to declare a new module for each subfolder? This makes QML a lot harder to use and in the end will lead to projects where having it dumped into one folder and a huge mess. I think this is inacceptable. Just imagine the requirement would be for the whole C++ project. Please don’t use subfolders 😅

---

<div class="post-metadata">

### Author: ![jobor](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jobor/32/1230_2.png) [@jobor](https://discourse.cmake.org/u/jobor)
#### Post date: [December 21, 2022, 9:25am UTC](https://discourse.cmake.org/t/dependency-cycle-with-qt6-qml/7099/5 "2022-12-21T09:25:39Z")

</div>

Thanks for the example. It’s indeed QTBUG-99768 you’re hitting.

> Question 1: Why doesn’t the docs mention it?

They mention it implicitly by having the executable target and the qt\_add\_qml\_module call in the same directory scope in every example.

It could be made clearer like suggested by Nils in QTBUG-99768.

> Question 2: Why is there no explicit warning about this

This can be done. Ideally, we’d find a way to lift the restriction.

> Question 3: Most importantly: How should this work in any real project?

You don’t have to “put everything into one folder”.

The restriction is that you have to put your add\_executable and qt\_add\_qml\_module calls into the same CMakeLists.txt (or at least the same CMake directory scope).

---

<div class="post-metadata">

### Author: ![Leon0402](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/l/ccd318/32.png) [@Leon0402](https://discourse.cmake.org/u/Leon0402)
#### Post date: [December 21, 2022, 9:45am UTC](https://discourse.cmake.org/t/dependency-cycle-with-qt6-qml/7099/6 "2022-12-21T09:45:03Z")

</div>

I’m a little bit confused right now what the issue really is about. Because the quote part:  
"You should keep the QML files in the same directory as the CMakeLists.txt that declares the module. "

Sounds like the qml files, i.e. main.qml, button.qml, \*.qml should all be in the same directory as the CMakeLists.txt or “I’m in a world of pain”.  
This means

```auto
- myfolder 
  - CMakeLists.txt 
  - main.qml <- Same folder
  - button.qml <- Same folder
  - subfolder 
     - subfolderComponent.qml <- Not the same folder!

```

But the issue / you are also talking about `qt_add_qml_module` and `add_executable` should only be in the same scope. And the rest (-\> The qml files themself) can be anywhere.

Did I just misunderstood the quote or are these two separate statements / problems?

And what would be the way forward? From what I can see the linked Issue QTBUG-99768 is not a bug, but a feature request, which is mainly motivated by the bug (as it turned out in the comments). So in general, this should probably be converted into a bug, because bugs have higher priority than feature requests for obvious reasons. And I think this bug is quite bad. (Ideally the restriction would be completly fixed, but the bad part is that it’s right now very easy to do wrong).  
Is this even a Qt Bug or a CMake Bug?

---

<div class="post-metadata">

### Author: ![Leon0402](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/l/ccd318/32.png) [@Leon0402](https://discourse.cmake.org/u/Leon0402)
#### Post date: [December 21, 2022, 9:52am UTC](https://discourse.cmake.org/t/dependency-cycle-with-qt6-qml/7099/7 "2022-12-21T09:52:57Z")

</div>

And I thought the issue here is also [[QTBUG-95200] qt6\_add\_qml\_module only works when used in same folder than the backing library - Qt Bug Tracker](https://bugreports.qt.io/browse/QTBUG-95200), which is marked as fixed. I don’t fully understand if it actually was fixed in the end. Maybe @craig.scott does know something about it here?

---

<div class="post-metadata">

### Author: ![jobor](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jobor/32/1230_2.png) [@jobor](https://discourse.cmake.org/u/jobor)
#### Post date: [December 21, 2022, 10:38am UTC](https://discourse.cmake.org/t/dependency-cycle-with-qt6-qml/7099/8 "2022-12-21T10:38:30Z")

</div>

That the terms folder/subdirectory/directory scope are used in different context doesn’t make things clearer I have to admit.

add\_subdirectory introduces a new directory scope, and that causes the problem you’re facing. You can work around the issue by avoiding add\_subdirectory for your qt\_add\_qml\_module call. You can do that by either putting qt\_add\_qml\_module back into your CMakeLists.txt that calls add\_executable or by including a .cmake file that calls qt\_add\_qml\_module.

Creating a follow-up bug report at [bugreports.qt.io](http://bugreports.qt.io) would be appropriate.

---

<div class="post-metadata">

### Author: ![Leon0402](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/l/ccd318/32.png) [@Leon0402](https://discourse.cmake.org/u/Leon0402)
#### Post date: [December 21, 2022, 11:36am UTC](https://discourse.cmake.org/t/dependency-cycle-with-qt6-qml/7099/9 "2022-12-21T11:36:03Z")

</div>

Thanks!

One thing I wonder as well is: Would it be useful to have additional commands like `target_sources` for qml modules?

With my regular C++ files I usually define a target in the top level source folder and then do add\_subdirectory and target sources to collect the files.  
I think it would be cool to do the same with qml files (and also C++ files, other ressources in a qml module)

---

<div class="post-metadata">

### Author: ![jobor](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jobor/32/1230_2.png) [@jobor](https://discourse.cmake.org/u/jobor)
#### Post date: [December 21, 2022, 1:26pm UTC](https://discourse.cmake.org/t/dependency-cycle-with-qt6-qml/7099/10 "2022-12-21T13:26:31Z")

</div>

You can certainly file a suggestion for that.  
With the current infrastructure it would be non-trivial to implement though.
