Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Solved Cannot open main.moc Cmake

    General and Desktop
    2
    3
    256
    Loading More Posts
    • 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.
    • mandruk1331
      mandruk1331 last edited by

      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 Reply Quote 0
      • Christian Ehrlicher
        Christian Ehrlicher Lifetime Qt Champion last edited by

        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 has to stay free or it will die.

        mandruk1331 1 Reply Last reply Reply Quote 3
        • mandruk1331
          mandruk1331 @Christian Ehrlicher last edited by

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

          Mandruk1331

          1 Reply Last reply Reply Quote 0
          • First post
            Last post