# Problem with CMAKE\_AUTOUIC

**URL:** <https://discourse.cmake.org/t/problem-with-cmake-autouic/2780>\
**Category:** Code\
**Tags:** os:windows, gen:vs\
**Created:** [February 17, 2021, 6:05am UTC](https://discourse.cmake.org/t/problem-with-cmake-autouic/2780 "2021-02-17T06:05:24Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![amaclean](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/85e7bf/32.png) [@amaclean](https://discourse.cmake.org/u/amaclean)\
**Post date:** [February 17, 2021, 6:05am UTC](https://discourse.cmake.org/t/problem-with-cmake-autouic/2780/1 "2021-02-17T06:05:24Z")

</div>

I am looking at rewriting some CMake scripts for Qt so Qt5 and Qt6 can be used, specifically those in the [VTK Examples](https://kitware.github.io/vtk-examples/site/).

I have run into a problem with Visual Studio and `CMAKE_AUTOUIC`. If `CMAKE_AUTOUIC` is set to `ON`, Visual Studio cannot find the `ui_*.h` files even though the files are in `build\[projectname]\[projectname]_autogen\include_Debug` and the path exists in the C/C++ additional include directories property pages for [projectname].

If I use **ninja** as the generator everything is OK as the `ui_*.h` files are in `[projectname]` folder.

To account for this I have had to do the following:

```cmake
# Automatically use qt's moc and uic compilers.
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
if (CMAKE_GENERATOR MATCHES "Visual Studio")
    set(CMAKE_AUTOUIC OFF)
endif()

```

and

```cmake
# Create code from a list of Qt designer ui files.
if(CMAKE_GENERATOR MATCHES "Visual Studio")
  # We only need to do this for Visual Studio
  if(qt_version EQUAL 6)
    qt_wrap_ui(${KIT}_UI_Hdrs ${${KIT}_UIS})
  else()
    qt5_wrap_ui(${KIT}_UI_Hdrs ${${KIT}_UIS})
  endif()
endif()

```

Is there a better solution than this?

I am using Visual Studio 16.8.5 and CMake 13.9.5.

---

<div class="post-metadata">

**Author:** ![amaclean](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/85e7bf/32.png) [@amaclean](https://discourse.cmake.org/u/amaclean)\
**Post date:** [February 17, 2021, 6:09am UTC](https://discourse.cmake.org/t/problem-with-cmake-autouic/2780/2 "2021-02-17T06:09:37Z")

</div>

I should add that the only thing I could find on the web was this: [Can’t include ui-file from linked library using CMake’s autouic](https://stackoverflow.com/questions/60920473/cant-include-ui-file-from-linked-library-using-cmakes-autouic)]([https://stackoverflow.com/questions/60920473/cant-include-ui-file-from-linked-library-using-cmakes-autouic](https://stackoverflow.com/questions/60920473/cant-include-ui-file-from-linked-library-using-cmakes-autouic)).

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [February 17, 2021, 2:47pm UTC](https://discourse.cmake.org/t/problem-with-cmake-autouic/2780/3 "2021-02-17T14:47:04Z")

</div>

Seems that the QtAutoGen developer is not on here…

Though I’m pretty sure there are QtAutoUic tests on Windows with Visual Studio, but maybe this is a hole in the test suite?

Cc: @brad.king

---

<div class="post-metadata">

**Author:** ![amaclean](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/85e7bf/32.png) [@amaclean](https://discourse.cmake.org/u/amaclean)\
**Post date:** [February 18, 2021, 8:34am UTC](https://discourse.cmake.org/t/problem-with-cmake-autouic/2780/4 "2021-02-18T08:34:52Z")

</div>

> [@amaclean](#):
>
> If I use **ninja** as the generator everything is OK as the `ui_*.h` files are in `[projectname]` folder.

Actually this is not true, they are found in `build/projectname]/[projectname]_autogen/include`. I must have had some old files in `build/[projectname]` from when CMAKE\_AUTOUIC was set to OFF.

So I’ll continue using the `qt_wrap_ui` stuff with `set(CMAKE_AUTOUIC OFF)`.

You can get around it by adding the `build/[projectname]/[projectname]_autogen/include` path to `include_directories` command, however for Visual Studio you would need to also add `../[projectname]_autogen/include_Debug`, `../[projectname]_autogen/include_Release` etc.

---

<div class="post-metadata">

**Author:** ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)\
**Post date:** [February 18, 2021, 11:19am UTC](https://discourse.cmake.org/t/problem-with-cmake-autouic/2780/5 "2021-02-18T11:19:17Z")

</div>

All of the Qt5Autogen tests in CMake run and pass with the Visual Studio generators. That covers AUTOUIC for Qt5, so this is expected to work in general. Please try to narrow down the failure to a small example.

I don’t think the CMake test suite has been updated to drive those tests for Qt6 though.

---

<div class="post-metadata">

**Author:** ![amaclean](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/85e7bf/32.png) [@amaclean](https://discourse.cmake.org/u/amaclean)\
**Post date:** [February 19, 2021, 6:24am UTC](https://discourse.cmake.org/t/problem-with-cmake-autouic/2780/6 "2021-02-19T06:24:06Z")

</div>

@brad, here is a small example Tested using qt5. The CMake variable `USE_UIC` is **ON** by default so it fails for me. When `USE_UIC` is **OFF** it works.

Remember to delete **`build/gui`** (if it exists) before running cmake. If you change the variable `USE_UIC` delete and re-run cmake.

[Qt6\_Qt5.zip](https://discourse.cmake.org/uploads/short-url/jGO6gpRPtyYr8YNU5hvEWydFOsH.zip) (7.0 KB)

---

<div class="post-metadata">

**Author:** ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)\
**Post date:** [February 19, 2021, 3:16pm UTC](https://discourse.cmake.org/t/problem-with-cmake-autouic/2780/7 "2021-02-19T15:16:10Z")

</div>

Thanks. With `Qt6_Qt5.zip` and VS 16.8 I get

```auto
gui\MainWindow.h(4,10): fatal error C1083: Cannot open include file: 'ui_MainWindow.h'

```

Please strip your example down further to avoid the Qt6/Qt5 switch (use Qt5 only), remove unnecessary Qt components, and remove subdirectories if possible. Then open [an issue](https://gitlab.kitware.com/cmake/cmake/-/issues) and attach the example.

---

<div class="post-metadata">

**Author:** ![amaclean](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/85e7bf/32.png) [@amaclean](https://discourse.cmake.org/u/amaclean)\
**Post date:** [February 20, 2021, 12:38am UTC](https://discourse.cmake.org/t/problem-with-cmake-autouic/2780/8 "2021-02-20T00:38:31Z")

</div>

@brad.king please see issue [21846](https://gitlab.kitware.com/cmake/cmake/-/issues/21846). I provided two examples, one where it fails (uses sub-directories) one where it works (no sub-directories).

---

<div class="post-metadata">

**Author:** ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)\
**Post date:** [February 23, 2021, 8:36pm UTC](https://discourse.cmake.org/t/problem-with-cmake-autouic/2780/9 "2021-02-23T20:36:41Z")

</div>

For reference, @jobor pointed out [in the issue](https://gitlab.kitware.com/cmake/cmake/-/issues/21846#note_908033) that this is not a bug.

@amaclean please account for that answer in the VTK Examples.

---

<div class="post-metadata">

**Author:** ![amaclean](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/a/85e7bf/32.png) [@amaclean](https://discourse.cmake.org/u/amaclean)\
**Post date:** [February 23, 2021, 10:17pm UTC](https://discourse.cmake.org/t/problem-with-cmake-autouic/2780/10 "2021-02-23T22:17:27Z")

</div>

I definitely will be accounting for that in the VTK Examples! Many thanks again.
