Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Cannot open main.moc Cmake
Forum Update on Monday, May 27th 2025

Cannot open main.moc Cmake

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 638 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • mandruk1331M Offline
    mandruk1331M Offline
    mandruk1331
    wrote on last edited by
    #1

    Hello, I have succesfully created a Cmake file to build my qt project, but my other project is a unit test project, the Cmake passess succesfully ( created a VS build ), but when I am trying to launch my UnitTest project it gives a compile error such as:

    Cannot open include file: 'main.moc': No such file or directory
    

    And here is my Cmake file:

    cmake_minimum_required(VERSION 3.14)
    project(UnitTests LANGUAGES CXX)
    
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
    find_package(Qt5 REQUIRED COMPONENTS Core Network Test )
    
    set(project_headers
        File1.h
        File2.h)
    
    set(project_sources 
           File1.cpp
           File2.cpp
    	main.cpp)
    
    qt5_wrap_cpp(project_sources_moc ${project_headers})
    
    add_executable(${PROJECT_NAME} ${project_headers} ${project_sources} 
        ${project_sources_moc})
    
    target_link_libraries(${PROJECT_NAME} 
        PUBLIC 
        Qt5::Core
        Qt5::Network
        Qt5::Test)
    

    How can I solve this issue? thank you

    Mandruk1331

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Either use CMAKE_AUTOMOC feature or tell cmake to create a moc file for your main.cpp by adding it to qt5_wrap_cpp or move the class definition into an own header.
      See also https://doc.qt.io/qt-5/cmake-manual.html

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      mandruk1331M 1 Reply Last reply
      3
      • Christian EhrlicherC Christian Ehrlicher

        Either use CMAKE_AUTOMOC feature or tell cmake to create a moc file for your main.cpp by adding it to qt5_wrap_cpp or move the class definition into an own header.
        See also https://doc.qt.io/qt-5/cmake-manual.html

        mandruk1331M Offline
        mandruk1331M Offline
        mandruk1331
        wrote on last edited by
        #3

        @Christian-Ehrlicher well that was it. Thank you)

        Mandruk1331

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved