Issue: “Could not find moc executable target Qt6::moc” when trying to build a C++ QT application with VS Code
I am able to successfully build & run my “MyProject” C++ QT6 application using QT Creator but it won’t build in VS Code…
Notes:
- Sanity Test passes ; I am able to successfully build and deploy a “Hello World” C++ QT application using VS Code
- CMake file has AUTOMOC ON
HOST
- Host / Dev Machine: Windows 11
- Visual Studio Code version 1.89.1
- Torizon IDE Extension v.2.4.2
- Qt Configure Extension v0.2.1
- Qt Tools Extension v0.11.0
CLIENT
- Device: Apalis iMX8
- BSP: Ixora
- Operating System:
- ID=torizon
- NAME=“TorizonCore”
- VERSION=“6.7.0-devel-202405+build.23 (kirkstone)”
- VERSION_ID=6.7.0-devel-202405-build.23
- PRETTY_NAME=“TorizonCore 6.7.0-devel-202405+build.23 (kirkstone)”
- DISTRO_CODENAME=“kirkstone”
- BUILD_ID=“23”
- ANSI_COLOR=“1;34”
- VARIANT=“Docker”
OUTPUT:
[main] Building folder: MyProject
[main] Configuring project: MyProject
[proc] Executing command: /usr/bin/cmake --no-warn-unused-cli -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -S/home/torizon/MyProject-B/home/torizon/MyProject/build -G “Unix Makefiles”
[cmake] Qt6_DIR is /usr/local/msvc2019_arm64/lib/cmake/Qt6
[cmake] Not searching for unused variables given on the command line.
[cmake] Project Name: MyProject
[cmake] Target Source: main.cpp
[cmake] Target Source: [more confidential info here]
[cmake] set_target_properties
[cmake] target_link_libraries
[cmake] CMAKE_INSTALL_LIBDIR = lib
[cmake] CMAKE_INSTALL_BINDIR = bin
[cmake] QT Version Major: 6
[cmake] QT Version Minor: 4
[cmake] CMake Error: AUTOMOC for target MyProject: Could not find moc executable target Qt6::moc
[cmake] CMake Generate step failed. Build files cannot be regenerated correctly.
[cmake] – Configuring done
[proc] The command: /usr/bin/cmake --no-warn-unused-cli -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -S/home/torizon/MyProject-B/home/torizon/MyProject/build -G “Unix Makefiles” exited with code: 1
CMakeLists.txt:
cmake_minimum_required(VERSION 3.25)
project(MyProject
LANGUAGES CXX
VERSION 0.1.0
DESCRIPTION Prototype
)set(CMAKE_PREFIX_PATH “/usr/local/msvc2019_arm64”)
set(Qt6_DIR “/usr/local/msvc2019_arm64/lib/cmake/Qt6”)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(QT_VERSION_MAJOR 6)
set(QT_VERSION_MINOR 4)message(“Qt6_DIR is ${Qt6_DIR}”)
#find_package(QT NAMES Qt6 REQUIRED COMPONENTS Core Gui Widgets SerialPort Charts Multimedia VirtualKeyboard)
find_package(QT NAMES Qt6 REQUIRED COMPONENTS Core Widgets SerialPort Charts Gui Multimedia VirtualKeyboard)set(PROJECT_SOURCES
main.cpp
[more *.cpp files]
[confidential.h files]
[confidential.ui files]
[confidential.qrc files]
)
message(“Project Name: ${PROJECT_NAME}”)add_executable(${PROJECT_NAME} WIN32
${PROJECT_SOURCES}
)foreach (file in ${PROJECT_SOURCES})
if ( file MATCHES “.*.cpp$”)
target_sources(${PROJECT_NAME} PRIVATE ${file})
message(“Target Source: ${file}”)
endif()
endforeach()set_target_properties(${PROJECT_NAME} PROPERTIES
MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
MACOSX_BUNDLE TRUE
WIN32_EXECUTABLE TRUE
AUTOMOC ON
AUTORCC ON
AUTOUIC ON
)
message(“set_target_properties”)
target_link_libraries(${PROJECT_NAME} PRIVATE
Qt6::Widgets
Qt6::SerialPort
Qt6::Charts
Qt6::Core
Qt6::Gui
Qt6::Multimedia
Qt6::VirtualKeyboard
)
message(“target_link_libraries”)
install(TARGETS ${PROJECT_NAME}
BUNDLE DESTINATION .
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
message(“CMAKE_INSTALL_LIBDIR = ${CMAKE_INSTALL_LIBDIR}”)
message(“CMAKE_INSTALL_BINDIR = ${CMAKE_INSTALL_BINDIR}”)message(“QT Version Major: ${QT_VERSION_MAJOR}”)
message(“QT Version Minor: ${QT_VERSION_MINOR}”)