C++ Qt + VS Code + Torizon IDE Extension + CMake....: Could not find moc executable target Qt6::moc


:red_circle: 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:

  • :green_circle: Sanity Test passes ; I am able to successfully build and deploy a “Hello World” C++ QT application using VS Code
  • :green_circle: 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}”)

Hi, I’m the Toradex AI Assistant. I’m in early beta, and I would appreciate your feedback.
The Toradex Support team will follow up with you shortly!

Did you like my answer?

:+1: :-1:


Hello,

The error you’re encountering, “Could not find moc executable target Qt6::moc,” typically indicates that CMake is unable to locate the Meta-Object Compiler (moc) that comes with Qt6. This is essential for processing Qt’s meta-object system, especially when AUTOMOC is enabled. Given that you’ve successfully built and run applications using Qt Creator, it suggests your Qt6 setup is generally correct, but there might be an issue with how it’s being recognized or accessed within the VS Code environment or specifically by CMake when invoked from VS Code.

Here are several steps to troubleshoot and potentially resolve this issue:

  1. Verify Qt6 Installation Path: Ensure that the Qt6 installation path is correctly specified in your CMakeLists.txt. The path you’ve set for Qt6_DIR should point to the directory containing Qt6Config.cmake or qt6-config.cmake. This is crucial for find_package to locate and use Qt6 correctly.

  2. CMake Cache: Sometimes, CMake cache might hold onto outdated paths or settings. Try clearing the CMake cache from within VS Code or manually deleting the CMakeCache.txt file in your build directory, then reconfigure your project.

  3. Environment Variables: Ensure that your environment variables are correctly set up to include paths to Qt6 tools like moc. For VS Code, you might need to adjust your system’s PATH environment variable or define the path within the VS Code settings or the CMakeLists.txt file.

  4. Qt6 CMake Integration: Double-check that the Qt6 integration with CMake is correctly set up. Your find_package call looks correct, but ensure that all required components are listed and that there are no typos or incorrect component names.

  5. VS Code Configuration: Review your VS Code configuration for the C++ and CMake extensions. Ensure that the extensions are up to date and correctly configured to use the correct CMake and compiler toolchain that matches your Qt6 setup.

  6. Check for Typos or Misconfigurations: A common issue can be simple typos or misconfigurations in the CMakeLists.txt file. Double-check all paths, target names, and component names for accuracy.

  7. Use Full Path for moc in CMakeLists.txt: As a last resort, if AUTOMOC is unable to locate moc, you can manually specify the full path to the

Please, also check if these links can help you solve your problem:

Qt on Torizon OS | Toradex Developer Center.

Greetings @miLee,

Did you make any changes to the default template configuration? Most importantly did you install any additional packages in your project? I’ll need to check but I don’t believe the containers used for compilation here have the Qt6 moc binary installed by default.

Best Regards,
Jeremias

Hi @jeremias.tx

  1. “default template configuration” - Are you referring to the CMakeLists.txt? Yes, I made changes. I added it to the above post.

  2. “did you install any additional packages in your project” - Yes, when I setup QT Creator (also known as QT Device), I had to install a Qt Board Support Package called “Qt for Device Creation QBSP 6.7.0”. This is needed for the project.

  3. " I’ll need to check but I don’t believe the containers used for compilation here have the Qt6 moc binary installed by default." - Would my HelloWorld C++ QT application have been built, deployed and run successfully via VS Code if it weren’t there?

After looking at your logs more closely something doesn’t seem right. For example in your logs I can see the following command is executed:

[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”

But no where in our default projects is such a command defined. Are you sure you are using our extensions to build and compile your application? Or are you just using standard VSCode cmake?

Furthermore your comment here:

Yes, when I setup QT Creator (also known as QT Device), I had to install a Qt Board Support Package called “Qt for Device Creation QBSP 6.7.0”. This is needed for the project.

This doesn’t do anything for our extension. All you did was install the Qt Board Support Package on your development machine. But, our extension works by compiling your application in a container. Therefore the packages on your host (outside of this container) don’t contribute anything to this process.

I did a quick test project using our Torizon extension and here’s a excerpt of the logs from when the application was being compiled:

Build logs
Executing task: DOCKER_HOST= docker run --rm -v /home/coj/test:/home/torizon/app cross-toolchain-arm64-test cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_COMPILER=aarch64-linux-gnu-g++ -DCMAKE_C_COMPILER=aarch64-linux-gnu-gcc -Bbuild-arm64 

WARNING: The requested image's platform (linux/arm64/v8) does not match the detected host platform (linux/amd64/v3) and no specific platform was requested
-- The CXX compiler identification is GNU 12.2.0
-- The C compiler identification is GNU 12.2.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/aarch64-linux-gnu-g++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/aarch64-linux-gnu-gcc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Success
-- Found Threads: TRUE  
-- Performing Test HAVE_STDATOMIC
-- Performing Test HAVE_STDATOMIC - Success
-- Found WrapAtomic: TRUE  
-- Performing Test HAVE_EGL
-- Performing Test HAVE_EGL - Success
-- Found EGL: /usr/include (found version "1.5") 
-- Performing Test HAVE_GLESv2
-- Performing Test HAVE_GLESv2 - Success
-- Found GLESv2: /usr/include  
-- Found XKB: /usr/lib/aarch64-linux-gnu/libxkbcommon.so (found suitable version "1.5.0", minimum required is "0.5.0") 
-- Found WrapVulkanHeaders: /usr/include  
-- Configuring done
-- Generating done
-- Build files have been written to: /home/torizon/app/build-arm64
 *  Terminal will be reused by tasks, press any key to close it. 

 *  Executing task: DOCKER_HOST= docker run --rm -v /home/coj/test:/home/torizon/app cross-toolchain-arm64-test cmake --build build-arm64 

WARNING: The requested image's platform (linux/arm64/v8) does not match the detected host platform (linux/amd64/v3) and no specific platform was requested
[  4%] Built target test_qmlimportscan
[ 17%] Built target test_tooling
[ 21%] Automatic MOC for target test
[ 26%] Built target test_autogen
[ 30%] Running AUTOMOC file extraction for target test
[ 30%] Built target test_automoc_json_extraction
[ 34%] Running moc --collect-json for target test
[ 39%] Automatic QML type registration for target test
[ 43%] Generating .rcc/qmlcache/test_QML/Scene.ui_qml.cpp
[ 47%] Generating .rcc/qmlcache/test_QML/main_qml.cpp
[ 52%] Running rcc for resource qmake_test
[ 56%] Building CXX object CMakeFiles/test.dir/test_autogen/mocs_compilation.cpp.o
[ 60%] Building CXX object CMakeFiles/test.dir/main.cpp.o
[ 65%] Building CXX object CMakeFiles/test.dir/test_qmltyperegistrations.cpp.o
[ 69%] Building CXX object CMakeFiles/test.dir/build-arm64/.rcc/qrc_qmake_test.cpp.o
[ 73%] Building CXX object CMakeFiles/test.dir/build-arm64/.rcc/qmlcache/test_qmlcache_loader.cpp.o
[ 78%] Building CXX object CMakeFiles/test.dir/build-arm64/.rcc/qmlcache/test_QML/main_qml.cpp.o
[ 82%] Building CXX object CMakeFiles/test.dir/build-arm64/.rcc/qmlcache/test_QML/Scene.ui_qml.cpp.o
[ 86%] Building CXX object CMakeFiles/test.dir/build-arm64/.rcc/qrc_test_raw_qml_0.cpp.o
[ 91%] Linking CXX executable bin/test
[100%] Built target test

Compared with your logs, the commands being executed are different using different arguments, flags, and everything. Also as you can see in my logs from the line Running AUTOMOC file extraction for target test moc seems to be working fine by default without me adding anything extra. I further confirmed by finding the moc binary in the SDK container used for compilation:

torizon@9d2aecf5af1c:~/app$ /usr/lib/qt6/libexec/moc -v
moc 6.4.2

Given all these findings, it would seem you’re not using our VSCode extension properly, if at all.

Best Regards,
Jeremias

Oh you’re right - I wasn’t using Torizon IDE.
I just tried to build with Torizon IDE and got this output:

[05-16 12:30:02.551] Scanning Torizon devices …

[05-16 12:30:02.572] Configuring debug id_rsa …

[05-16 12:30:02.573] Debug id_rsa OK

[05-16 12:30:02.574] Torizon IDE extension activated

[05-16 12:30:03.011] Checking host ip sanity …

[05-16 12:30:03.939] Debian bookworm packages list load OK

[05-16 12:30:04.152] Host ip is OK

[05-16 12:30:04.164] Device 192.168.0.105 set as Docker Host

[05-16 12:40:55.982] ERROR :: build-configure-amd64-local failed with exit code 1

[05-16 19:32:00.639] ERROR :: build-configure-amd64-local failed with exit code 1

[05-16 19:32:35.654] ERROR :: build-configure-arm64 failed with exit code 1

I will work on this. Thank you!

Make sure to reference our documentation on using our VSCode extension: Visual Studio Code IDE Extension | Toradex Developer Center

Best Regards,
Jeremias

1 Like

@jeremias.tx I know what you mean by template, now.
Yes, I started off with the Torizon Qt template when I created the Torizon project in VS Code. I verified that it can build, deploy and run the “Hello World” program.
Then, I added to the same template/project my own files (.h, .cpp, .ui, .qrc) and updated the CMakeLists.txt and main.cpp. This is when build error occurs.

Well there’s a lot to potentially dissect here.

This is when build error occurs.

First of all, what’s the build error? Is it the same as the first one you shared? Different? What are the exact logs of the error?

I added to the same template/project my own files (.h, .cpp, .ui, .qrc) and updated the CMakeLists.txt and main.cpp

You’ve changed a lot of files in the project all at once, this could make it difficult to determine what is causing the issue. If you could change things slowly in smaller steps, it would make it easier in theory to determine what change causes the error.

updated the CMakeLists.txt

Did you update the CMakeLists.txt file to be the same as what you shared in your initial post?

If you did I can already say this probably would cause issues with our extension. For example I can see in your initial CMakeLists.txt that you shared you set several file-paths . Setting these file-paths do not really make sense in our extension. As I said previously our extension works by cross compiling your application within a container. A container has it’s own filesystem. Therefore setting file-paths would not make sense in this context.

Typically from what I’ve seen from other customers they need only to install the libraries they need for their application. Then they customize their project to find and link these libraries against their application. For example see how another customer configured their project in this other thread: Visual Studio Code Qt template

Best Regards,
Jeremias

@jeremias.tx Thank you for the reply. I will get back to you after I resolve this issue here.