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. Unable to run QT Quick application built with CMake and static linking of QT libraries in Visual Studio
Forum Update on Monday, May 27th 2025

Unable to run QT Quick application built with CMake and static linking of QT libraries in Visual Studio

Scheduled Pinned Locked Moved General and Desktop
qtquickcmakestaticvisual studio
3 Posts 1 Posters 5.8k 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.
  • P Offline
    P Offline
    PeJo
    wrote on last edited by
    #1

    Hi,
    I am working on a project where I use CMake to build the projects. I normally use Visual Studio for development. The project consist of multiple software components that shall run on multiple platforms. The GUI applications should also be able to run on multiple platforms where Windows, OSX, Android and IOS would be the important one.

    I would like to do this with a static build, but I have not been able to get it to work. I think I have exhausted my web search capabilities. I have made a simple example that illustrates the problem and the closest I could come to get something working.

    I have simple qml file main.qml, I have a main.cpp that looks like this:

    #include <QApplication>
    #include <QQmlApplicationEngine>
    #include <QtPlugin>
    
    #ifdef USE_STATIC_QT
    Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin)
    #include <QQmlExtensionPlugin>
    Q_IMPORT_PLUGIN(QtQuick2Plugin)
    Q_IMPORT_PLUGIN(QtQuickControlsPlugin)
    #endif
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        QQmlApplicationEngine engine;
    #ifdef USE_STATIC_QT
        //Without these lines I get: QQmlApplicationEngine failed to load component qrc : / main.qml : 1 module "QtQuick" plugin "qtquick2plugin" not found
        QObject * plugin = qt_static_plugin_QtQuick2Plugin().instance();
        QQmlExtensionPlugin* extensionPlugin = qobject_cast<QQmlExtensionPlugin*>(plugin); //This returns NULL
        extensionPlugin->registerTypes("QtQuick");
    #endif
        engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
    
        return app.exec();
    }
    

    My CMakeList.txt looks like this:

    cmake_minimum_required(VERSION 2.8.3)
    project(StaticQmlCMake)
    
    cmake_policy(SET CMP0020 NEW)
    ADD_DEFINITIONS(-DUNICODE)
    ADD_DEFINITIONS(-D_UNICODE)
    
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
    
    set(CMAKE_AUTOMOC ON)
    
    find_package(Qt5Quick REQUIRED)
    find_package(Qt5Qml REQUIRED)
    find_package(OpenGL REQUIRED)
    find_package(Qt5Widgets REQUIRED)
    
    qt5_add_resources(RESOURCES qml.qrc) 
        
    add_executable(StaticQmlCMake WIN32 main.cpp ${RESOURCES})
    
    if(USE_STATIC_QT)
    
        find_library(HARFBUZZLIB_REL qtharfbuzzng)
        find_library(HARFBUZZLIB_DEB qtharfbuzzngd)
        set(HARFBUZZLIB optimized ${HARFBUZZLIB_REL} debug ${HARFBUZZLIB_DEB})
    
        find_library(QT5PLATFORMSUPPORT_REL Qt5PlatformSupport)
        find_library(QT5PLATFORMSUPPORT_DEB Qt5PlatformSupportd)
        set(QT5PLATFORMSUPPORT optimized ${QT5PLATFORMSUPPORT_REL} debug ${QT5PLATFORMSUPPORT_DEB})
    
        get_target_property(QtQuick2PluginLoc Qt5::QtQuick2Plugin LOCATION)
        get_filename_component(PLUGINPATH ${QtQuick2PluginLoc} DIRECTORY)
        set(QMLDBGTCP optimized "${PLUGINPATH}/qmldbg_tcp.lib" debug "${PLUGINPATH}/qmldbg_tcpd.lib")
        get_filename_component(PLUGINPATH ${PLUGINPATH} DIRECTORY)
    
        if (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
        set(PLATFORMLIB optimized "${PLUGINPATH}/platforms/qwindows.lib"
                        debug "${PLUGINPATH}/platforms/qwindowsd.lib" 
                        imm32.lib
                        winmm.lib
                        Ws2_32.lib)
        endif()
    
        get_filename_component(QMLPATH ${PLUGINPATH} DIRECTORY)
        set(QMLPATH "${QMLPATH}/qml")
        set(QTQUICK_CONTROLS_PLUGIN optimized "${QMLPATH}/QtQuick/Controls/qtquickcontrolsplugin.lib" debug "${QMLPATH}/QtQuick/Controls/qtquickcontrolsplugind.lib")
    
        set(STATIC_QT_LIBRARIES ${HARFBUZZLIB} ${QT5PLATFORMSUPPORT} Qt5::QtQuick2Plugin ${QTQUICK_CONTROLS_PLUGIN} ${QMLDBGTCP} ${PLATFORMLIB})
        
        ADD_DEFINITIONS(-DUSE_STATIC_QT)
    endif()
    
    target_link_libraries(StaticQmlCMake Qt5::Gui Qt5::Widgets Qt5::Core Qt5::Qml Qt5::Quick ${OPENGL_LIBRARIES} ${STATIC_QT_LIBRARIES})
    

    I built QT static with msvc compiler and used the following configuration:
    configure -c++11 -mp -debug-and-release -static -nomake tests -nomake examples -prefix C:\Qt\Qt5.4.1\5.4\msvc2013-compact-static-x86 -platform win32-msvc2013 -opengl desktop -no-icu -skip webkit

    I have the problem that qtquick2plugin is not loaded and after searching the web and this forum I found some solution where one should add the following line of code:

    qobject_cast<QQmlExtensionPlugin*>(qt_static_plugin_QtQuick2Plugin().instance())->registerTypes("QtQuick");
    

    This does not seem to work for me and cast operation fails.

    So what am I doing wrong?
    The example project can be downloaded here

    1 Reply Last reply
    0
    • P Offline
      P Offline
      PeJo
      wrote on last edited by PeJo
      #2

      It seems like the parts of the CMakeList.txt file are missing in my previous post because I put it in a code block.
      So her it is again:

      cmake_minimum_required(VERSION 2.8.3)
      project(StaticQmlCMake)

      cmake_policy(SET CMP0020 NEW)
      ADD_DEFINITIONS(-DUNICODE)
      ADD_DEFINITIONS(-D_UNICODE)

      set(CMAKE_INCLUDE_CURRENT_DIR ON)

      set(CMAKE_AUTOMOC ON)

      find_package(Qt5Quick REQUIRED)
      find_package(Qt5Qml REQUIRED)
      find_package(OpenGL REQUIRED)
      find_package(Qt5Widgets REQUIRED)

      qt5_add_resources(RESOURCES qml.qrc)

      add_executable(StaticQmlCMake WIN32 main.cpp ${RESOURCES})

      if(USE_STATIC_QT)

      find_library(HARFBUZZLIB_REL qtharfbuzzng)
      find_library(HARFBUZZLIB_DEB qtharfbuzzngd)
      set(HARFBUZZLIB optimized ${HARFBUZZLIB_REL} debug ${HARFBUZZLIB_DEB})

      find_library(QT5PLATFORMSUPPORT_REL Qt5PlatformSupport)
      find_library(QT5PLATFORMSUPPORT_DEB Qt5PlatformSupportd)
      set(QT5PLATFORMSUPPORT optimized ${QT5PLATFORMSUPPORT_REL} debug ${QT5PLATFORMSUPPORT_DEB})

      get_target_property(QtQuick2PluginLoc Qt5::QtQuick2Plugin LOCATION)
      get_filename_component(PLUGINPATH ${QtQuick2PluginLoc} DIRECTORY)
      set(QMLDBGTCP optimized "${PLUGINPATH}/qmldbg_tcp.lib" debug "${PLUGINPATH}/qmldbg_tcpd.lib")
      get_filename_component(PLUGINPATH ${PLUGINPATH} DIRECTORY)

      if (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
      set(PLATFORMLIB optimized "${PLUGINPATH}/platforms/qwindows.lib"
      debug "${PLUGINPATH}/platforms/qwindowsd.lib"
      imm32.lib
      winmm.lib
      Ws2_32.lib)
      endif()

      get_filename_component(QMLPATH ${PLUGINPATH} DIRECTORY)
      set(QMLPATH "${QMLPATH}/qml")
      set(QTQUICK_CONTROLS_PLUGIN optimized "${QMLPATH}/QtQuick/Controls/qtquickcontrolsplugin.lib" debug "${QMLPATH}/QtQuick/Controls/qtquickcontrolsplugind.lib")

      set(STATIC_QT_LIBRARIES ${HARFBUZZLIB} ${QT5PLATFORMSUPPORT} Qt5::QtQuick2Plugin ${QTQUICK_CONTROLS_PLUGIN} ${QMLDBGTCP} ${PLATFORMLIB})

      ADD_DEFINITIONS(-DUSE_STATIC_QT)
      endif()

      target_link_libraries(StaticQmlCMake Qt5::Gui Qt5::Widgets Qt5::Core Qt5::Qml Qt5::Quick ${OPENGL_LIBRARIES} ${STATIC_QT_LIBRARIES})

      1 Reply Last reply
      0
      • P Offline
        P Offline
        PeJo
        wrote on last edited by
        #3

        For some reason things disappear after the dollar signs in my posts so if you are interested in helping and need the CMakeList.txt file, you need to download the project from the link in my first post.

        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