How do I configure Qt 6 includes correctly in a CMake project with VS Code?
Asked by
VelvetMango42
I'm setting up a small Qt 6 C++ application in VS Code on Windows or Linux, but VS Code reports errors such as "QApplication: No such file or directory" for Qt headers. CMake is installed, and my project uses CMake with Qt6 Widgets:
```cmake
cmake_minimum_required(VERSION 3.16)
project(MyQtApp VERSION 1.0.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Qt6 REQUIRED COMPONENTS Widgets)
qt_standard_project_setup()
qt_add_executable(MyQtApp
mainwindow.ui
mainwindow.cpp
main.cpp
)
target_link_libraries(MyQtApp PRIVATE Qt6::Widgets)
set_target_properties(MyQtApp PROPERTIES
WIN32_EXECUTABLE ON
MACOSX_BUNDLE ON
)
```
My source includes `` and ``. I have installed the CMake Tools and Qt-related VS Code extensions. CMake configures successfully in a `build` directory, and I can run `cmake --build build`. Qt is installed as well; `qmake --version` reports Qt 6.11.2. What else is needed to make the Qt headers resolve correctly in VS Code and build reliably?
Answers
The VS Code extensions donโt install Qt itselfโthey only add editor and CMake integration. Make sure a complete Qt development package is installed, including the headers and CMake package files. Since CMake configures successfully, the remaining error may only be IntelliSense.
Use the CMake Tools extension to select the correct compiler and configure the project from its source directory. It also helps to enable `CMAKE_EXPORT_COMPILE_COMMANDS` so VS Code can read the actual include paths:
```cmake
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
```
Then delete the build directory, configure again, and select the generated `compile_commands.json` or the active CMake configuration in VS Code. The Qt include directories should come from the `Qt6::Widgets` target; manually adding `/usr/include` or random Qt paths is usually unnecessary.
Reply to this answer
Register the actual Qt installation with the Qt extension and make sure CMake is using the same installation. The extension pack is not a substitute for installing Qt. If multiple Qt versions are present, CMake may find one version while VS Code is configured for another.
After selecting the Qt installation and CMake kit, run configure again. Also check that `find_package(Qt6 REQUIRED COMPONENTS Widgets)` completes without warnings and that the build directory is the one VS Code is using. If configuration fails to find Qt, set `CMAKE_PREFIX_PATH` to the Qt installation prefix, but donโt add individual header directories to the source code.
Reply to this answer
Your CMake structure and `QApplication` code are basically correct. A successful CMake configure means the target receives Qtโs include paths through `target_link_libraries(MyQtApp PRIVATE Qt6::Widgets)`. Build from the project root with `cmake -S . -B build` followed by `cmake --build build`.
If the compiler itself says the header is missing, verify that the Qt development headers are installedโnot just the runtime or `qmake`. If only VS Code complains, reload the window or reset the CMake/IntelliSense cache after reconfiguring.
Reply to this answer
Add your answer
Answers are reviewed before publication.
BrightCedar18: The important distinction is whether the error appears while compiling or only as a red underline in the editor. If `cmake --build build` succeeds, itโs an IntelliSense configuration issue rather than a C++ or Qt problem.