Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Installation and Deployment
  4. How to creat Qt static build as a /MT runtime library
Forum Updated to NodeBB v4.3 + New Features

How to creat Qt static build as a /MT runtime library

Scheduled Pinned Locked Moved Solved Installation and Deployment
12 Posts 5 Posters 2.7k Views 1 Watching
  • 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.
  • hskoglundH Offline
    hskoglundH Offline
    hskoglund
    wrote on last edited by
    #2

    Hi, for Visual Studio I'm only building for Static Release mode so I don't know about Debug mode, but for Release mode I need to add this:
    set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")

    1 Reply Last reply
    0
    • M Offline
      M Offline
      Michael418
      wrote on last edited by
      #3

      @hskoglund Hi, thank you for your answer but set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT") does not work for me. I think the problem is that some modules like QGifPlugin was not built as a /MT runtime library. Maybe some problems in pre build configurations for Qt sources before creating a static build. My CmakeLists.txt looks like this:

      project(qt_app)
      cmake_minimum_required(VERSION 3.8)
      set(CMAKE_CXX_STANDARD 20)
      
      set(QT_PATH "D:/Qt/6.2/msvc2022_64")
      set(CMAKE_PREFIX_PATH ${QT_PATH})
      
      add_compile_options($<$<CONFIG:Debug>:/MTd>$<$<NOT:$<CONFIG:Debug>>:/MT>) #mt
      
      include("${QT_PATH}/lib/cmake/Qt6/Qt6Config.cmake")
      include("${QT_PATH}/lib/cmake/Qt6Core/Qt6CoreConfig.cmake")
      include("${QT_PATH}/lib/cmake/Qt6Widgets/Qt6WidgetsConfig.cmake")
      include("${QT_PATH}/lib/cmake/Qt6Gui/Qt6GuiConfig.cmake")
      
      set(BUILD_STATIC ON CACHE BOOL "" FORCE)
      set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
      set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zc:__cplusplus")
      
      if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
          set(AUTOMOC_EXECUTABLE "${QT_PATH}/bin/moc.exe")
          set(AUTORCC_EXECUTABLE "${QT_PATH}/bin/rcc.exe")
      elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
          set(AUTOMOC_EXECUTABLE "${QT_PATH}/libexec/moc")
          set(AUTORCC_EXECUTABLE "${QT_PATH}/libexec/rcc")
      elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
          set(AUTOMOC_EXECUTABLE "${QT_PATH}/libexec/moc")
          set(AUTORCC_EXECUTABLE "${QT_PATH}/libexec/rcc")
      else()
          message(WARNING "CMAKE_SYSTEM_NAME is undefined")
      endif()
      
      if(NOT TARGET Qt6::moc)
          add_executable(Qt6::moc IMPORTED)
      endif()
      if(NOT TARGET Qt6::rcc)
          add_executable(Qt6::rcc IMPORTED)
      endif()
      
      set_target_properties(Qt6::moc PROPERTIES IMPORTED_LOCATION "${AUTOMOC_EXECUTABLE}")
      set_target_properties(Qt6::rcc PROPERTIES IMPORTED_LOCATION "${AUTORCC_EXECUTABLE}")
      
      set(Qt6Core_VERSION_MAJOR 6)
      set(Qt6Core_VERSION_MINOR 2)
      
      set(CMAKE_AUTOMOC ON)
      set(CMAKE_AUTORCC ON)
      set(CMAKE_INCLUDE_CURRENT_DIR ON)
      
      if(CMAKE_VERSION VERSION_LESS "3.7.0")
          set(CMAKE_INCLUDE_CURRENT_DIR ON)
      endif()
      
      add_executable (${PROJECT_NAME}
          mainwindow.h
          main.cpp
          mainwindow.cpp
      )
      
      target_link_libraries(${PROJECT_NAME}
          PUBLIC
          Dwrite
          Secur32
          Iphlpapi
          Winhttp
          Dnsapi
          windowsapp
          version
          userenv
          Winmm
          Wtsapi32
          Imm32
          dwmapi
          Ws2_32
          Qt6::Core
          Qt6::Widgets
          Qt6::Gui
      )
      
      target_include_directories(${PROJECT_NAME}
          PUBLIC ${QT_PATH}/include
          PUBLIC ${QT_PATH}/include/QtCore
          PUBLIC ${QT_PATH}/include/QtWidgets
          PUBLIC ${QT_PATH}/include/QtGui
      )
      
      1 Reply Last reply
      0
      • hskoglundH Offline
        hskoglundH Offline
        hskoglund
        wrote on last edited by
        #4

        I see you're on Qt 6.2.4, have you tried Qt 6.3.1?
        I think for that version specifying /MT is not needed any more (when building Static MSVC versions).

        1 Reply Last reply
        0
        • M Offline
          M Offline
          Michael418
          wrote on last edited by
          #5

          @hskoglund I have tried recently Qt 6.3.1 with the same configure parameters for the build that I mentioned in the description, and I got the same issues.

          D 1 Reply Last reply
          0
          • M Michael418

            @hskoglund I have tried recently Qt 6.3.1 with the same configure parameters for the build that I mentioned in the description, and I got the same issues.

            D Offline
            D Offline
            DerReisende
            wrote on last edited by DerReisende
            #6

            @Michael418 Have you tried building just the static release version? That should succeed. I couldn't get a mixed build to work as well AFAIK you need to specify /MTd for debug builds.

            I used shared debug version and just built my app with static release.

            1 Reply Last reply
            2
            • S Offline
              S Offline
              SimonSchroeder
              wrote on last edited by
              #7

              The error messages tell that you are compiling your code with parameters normally used for release, but link to the Qt debug libraries. Are you compiling a debug or release version of your software? If you are compiling a debug version you should use /MTd instead of /MT like the Qt libraries do. If you are compiling a release version we need to figure out why it links to the debug version. Which of the two is it?

              1 Reply Last reply
              1
              • M Offline
                M Offline
                Michael418
                wrote on last edited by
                #8

                @DerReisende @SimonSchroeder thanks, building just the static release version fixed the problem of compiling. However, exe file still has dependencies from windows DLL files but I need to make a static executable file that can be run on any machine. Is there a way to avoid such dependencies?

                UxTheme.dll
                DWrite.dll
                dwmapi.dll
                GDI32.dll
                IMM32.dll
                OLEAUT32.dll
                WTSAPI32.dll
                api-ms-win-core-synch-l1-2-0.dll
                USERENV.dll
                ADVAPI32.dll
                KERNEL32.dll
                NETAPI32.dll
                ole32.dll
                SHELL32.dll
                USER32.dll
                VERSION.dll
                WINMM.dll
                WS2_32.dll
                api-ms-win-core-winrt-error-l1-1-0.dll
                api-ms-win-core-winrt-l1-1-0.dll
                api-ms-win-core-winrt-error-l1-1-1.dll
                api-ms-win-core-winrt-string-l1-1-0.dll
                
                JonBJ D 2 Replies Last reply
                0
                • M Michael418

                  @DerReisende @SimonSchroeder thanks, building just the static release version fixed the problem of compiling. However, exe file still has dependencies from windows DLL files but I need to make a static executable file that can be run on any machine. Is there a way to avoid such dependencies?

                  UxTheme.dll
                  DWrite.dll
                  dwmapi.dll
                  GDI32.dll
                  IMM32.dll
                  OLEAUT32.dll
                  WTSAPI32.dll
                  api-ms-win-core-synch-l1-2-0.dll
                  USERENV.dll
                  ADVAPI32.dll
                  KERNEL32.dll
                  NETAPI32.dll
                  ole32.dll
                  SHELL32.dll
                  USER32.dll
                  VERSION.dll
                  WINMM.dll
                  WS2_32.dll
                  api-ms-win-core-winrt-error-l1-1-0.dll
                  api-ms-win-core-winrt-l1-1-0.dll
                  api-ms-win-core-winrt-error-l1-1-1.dll
                  api-ms-win-core-winrt-string-l1-1-0.dll
                  
                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by
                  #9

                  @Michael418
                  So far as I know (unless a Windows expert corrects me) these are all (subject to verification for some?) Windows System files. They must be present on the target machine as part of Windows, there is no "static" version of these.

                  1 Reply Last reply
                  1
                  • M Michael418

                    @DerReisende @SimonSchroeder thanks, building just the static release version fixed the problem of compiling. However, exe file still has dependencies from windows DLL files but I need to make a static executable file that can be run on any machine. Is there a way to avoid such dependencies?

                    UxTheme.dll
                    DWrite.dll
                    dwmapi.dll
                    GDI32.dll
                    IMM32.dll
                    OLEAUT32.dll
                    WTSAPI32.dll
                    api-ms-win-core-synch-l1-2-0.dll
                    USERENV.dll
                    ADVAPI32.dll
                    KERNEL32.dll
                    NETAPI32.dll
                    ole32.dll
                    SHELL32.dll
                    USER32.dll
                    VERSION.dll
                    WINMM.dll
                    WS2_32.dll
                    api-ms-win-core-winrt-error-l1-1-0.dll
                    api-ms-win-core-winrt-l1-1-0.dll
                    api-ms-win-core-winrt-error-l1-1-1.dll
                    api-ms-win-core-winrt-string-l1-1-0.dll
                    
                    D Offline
                    D Offline
                    DerReisende
                    wrote on last edited by
                    #10

                    @Michael418 You cannot avoid dependencies to windows system dlls.
                    There is a reason why qt has a minimum OS requirement.

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      Michael418
                      wrote on last edited by
                      #11

                      @DerReisende Is there official information that Qt requires specific DLLs from Windows? I just need proof for my project if I can not avoid some DDLs.

                      JonBJ 1 Reply Last reply
                      0
                      • M Michael418

                        @DerReisende Is there official information that Qt requires specific DLLs from Windows? I just need proof for my project if I can not avoid some DDLs.

                        JonBJ Online
                        JonBJ Online
                        JonB
                        wrote on last edited by JonB
                        #12

                        @Michael418 said in How to creat Qt static build as a /MT runtime library:

                        Is there official information that Qt requires specific DLLs from Windows?

                        No. And btw this is not a Qt-specific restriction, it's Windows.

                        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