Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. CMake: How do I add files to application target using multiple CMakeLists.txt files?
Forum Updated to NodeBB v4.3 + New Features

CMake: How do I add files to application target using multiple CMakeLists.txt files?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 1 Posters 932 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.
  • T Offline
    T Offline
    Toppler
    wrote on 4 Apr 2023, 11:56 last edited by
    #1

    When I add a QML_ELEMENT class (cpp and h) files to cmake variable CPP_AND_H_PROJECT_FILES and then use it in qt_add_executable, everything works fine.

    But when I add the same two files in that same variable in a CMakeLists.txt that is located in the same directory where the files are, Qt Creator and QQmlApplicationEngine do not recognize the class/files anymore. Everything still compiles correctly and I can even use a static member of that class in main.

    Qt Creator Projects tree does not show the files and QQmlApplicationEngine runtime error says:

    QQmlApplicationEngine failed to load component
    qrc:/d230404b_project_test/Main.qml:12:5: TestValue is not a type

    How do I tell QT Creator and QQmlApplicationEngine that the files are added?

    github.com/T0pp1er/d230404b_project_test

    cmake_minimum_required(VERSION 3.16)
    
    project(d230404b_project_test VERSION 0.1 LANGUAGES CXX)
    
    set(CMAKE_CXX_STANDARD_REQUIRED ON)
    
    find_package(Qt6 6.4 REQUIRED COMPONENTS Quick)
    
    qt_standard_project_setup()
    
    set(CPP_AND_H_PROJECT_FILES main.cpp)
    
    
    ### THIS WORKS ##########################################
    
    # set(CPP_AND_H_PROJECT_FILES ${CPP_AND_H_PROJECT_FILES}
    #     values/testvalue.h
    #     values/testvalue.cpp
    # )
    # include_directories(values)
    
    ### THIS DOES NOT WORK (but compiles correctly) #########
    
    set(VALUES_DIR values)
    add_subdirectory(${VALUES_DIR})
    
            # #values/CMakeLists.txt
            #
            # set(CPP_AND_H_PROJECT_FILES ${CPP_AND_H_PROJECT_FILES}
            #     ${VALUES_DIR}/testvalue.h
            #     ${VALUES_DIR}/testvalue.cpp
            # )
            # include_directories(${VALUES_DIR})
    
    #########################################################
    
    
    qt_add_executable(appd230404b_project_test
        ${CPP_AND_H_PROJECT_FILES}
    )
    
    qt_add_qml_module(appd230404b_project_test
        URI d230404b_project_test
        VERSION 1.0
        QML_FILES Main.qml
    )
    
    set_target_properties(appd230404b_project_test 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
    )
    
    target_link_libraries(appd230404b_project_test
        PRIVATE Qt6::Quick
    )
    
    install(TARGETS appd230404b_project_test
        BUNDLE DESTINATION .
        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
    
    
    1 Reply Last reply
    0
    • T Offline
      T Offline
      Toppler
      wrote on 7 Apr 2023, 04:51 last edited by
      #2

      The magical entity that creates ".qmltypes" file and registers components seems to only see one CMakeLists.txt file, despite add_subdirectory. You need to manually tell in the same CMakeLists.txt where qt_add_qml_module is where the source files are using SOURCES parameter.

      I just tried to have something similar to qmake project include (.pri) files.

      Seems like one CMakeLists.txt file per target is the simplest way to.

      Here is something that works:

      # CMakeLists.txt
      
      cmake_minimum_required(VERSION 3.16)
      
      project(d230406a_project_test_b VERSION 0.1 LANGUAGES CXX)
      
      set(CMAKE_CXX_STANDARD_REQUIRED ON)
      
      find_package(Qt6 6.4 REQUIRED COMPONENTS Quick)
      
      qt_standard_project_setup()
      
      set(CPP_AND_H_PROJECT_FILES)
      
      set(VALUES_DIR values)
      
      add_subdirectory(${VALUES_DIR})
              # values/CMakeLists.txt:
              # set(CPP_AND_H_PROJECT_FILES ${CPP_AND_H_PROJECT_FILES}
              #     ${VALUES_DIR}/testvalue.h
              #     ${VALUES_DIR}/testvalue.cpp
              # )
      include_directories(${VALUES_DIR})
      
      qt_add_executable(appd230406a_project_test_b
          main.cpp ${CPP_AND_H_PROJECT_FILES}
      )
      
      qt_add_qml_module(appd230406a_project_test_b
          URI d230406a_project_test_b
          VERSION 1.0
          QML_FILES Main.qml
          SOURCES values/testvalue.h values/testvalue.cpp
          #SOURCES ${CPP_AND_H_PROJECT_FILES} # THIS DOES NOT WORK
      )
      
      set_target_properties(appd230406a_project_test_b 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
      )
      
      target_link_libraries(appd230406a_project_test_b
          PRIVATE Qt6::Quick
      )
      
      install(TARGETS appd230406a_project_test_b
          BUNDLE DESTINATION .
          LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})
      
      
      # values/CmakeLists.txt
      
      set(CPP_AND_H_PROJECT_FILES ${CPP_AND_H_PROJECT_FILES}
          ${VALUES_DIR}/testvalue.h
          ${VALUES_DIR}/testvalue.cpp
      )
      
      #include_directories(${VALUES_DIR}) # THIS DOES NOT WORK
      
      1 Reply Last reply
      0
      • T Toppler has marked this topic as solved on 7 Apr 2023, 04:52

      2/2

      7 Apr 2023, 04:51

      • Login

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