Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Can I create a static library from QML and use that with my executable?



  • I have a CMake Qt project. It used to be qmake, but it's being slowly converted to CMake. I am trying to put some commonly-used QML into a static library and then allowing applications to link against this static library. Compiling and linking works, but I get run-time errors saying e.g. module "BCControls" is not installed.

    The CMake for the static library is:

    add_library(BCControls STATIC)
    
    qt5_add_resources(RESOURCES bccontrols.qrc)
    
    target_sources(BCControls
        PRIVATE
        ${RESOURCES}
        )
    

    The application's CMake is:

    add_executable(PerMain)
    
    qt5_add_resources(RESOURCES qml.qrc)
    
    target_include_directories(PerMain PRIVATE .)
    
    target_sources(PerMain
        PRIVATE
        ${RESOURCES}
        main.cpp
        PerMainApp.cpp
        )
    
    target_link_libraries(PerMain
        PRIVATE
        Qt5::Core
        PrinterIF
        BQIF
        Properties
        AppData
        CommonBaseErrorDetectors
        BCControls
        )
    

    Everything works fine if I put the bccontrols.qrc in the PerMain/CMakeLists.txt like this:

    add_executable(PerMain)
    
    qt5_add_resources(RESOURCES
        qml.qrc
        ../../QtCommon/BCControls/bccontrols.qrc
        )
    
    target_include_directories(PerMain PRIVATE .)
    
    target_sources(PerMain
        PRIVATE
        ${RESOURCES}
        main.cpp
        PerMainApp.cpp
        )
    
    target_link_libraries(PerMain
        PRIVATE
        Qt5::Core
        PrinterIF
        BQIF
        Properties
        AppData
        CommonBaseErrorDetectors
    #    BCControls
        )
    

    Is it possible to use a static library in the way I'm trying to use it? If so, what am I missing?

    Thanks!


  • Moderators

    When you are adding resources to a library, you need to init them manually.See the docs: https://doc.qt.io/qt-5/resources.html#using-resources-in-a-library



  • @sierdzio Thank you.