How to creat Qt static build as a /MT runtime library
-
wrote on 8 Sept 2022, 08:50 last edited by
I have created a static Qt build on Windows 10 by using Visual Studio 2022. I am trying to create Qt static application via CMake by linking Qt and its modules as a /MT runtime library. In CMake I am linking the next Qt modules: Qt6::Core, Qt6::Widgets, Qt6::Gui. However, I get errors during linking:
QWindowsVistaStylePlugin_init.cpp.obj : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '2' doesn't match value '0' in mocs_compilation.cpp.obj QWindowsVistaStylePlugin_init.cpp.obj : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MT_StaticRelease' in mocs_compilation.cpp.obj QGifPlugin_init.cpp.obj : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '2' doesn't match value '0' in mocs_compilation.cpp.obj QGifPlugin_init.cpp.obj : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MT_StaticRelease' in mocs_compilation.cpp.obj QICNSPlugin_init.cpp.obj : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '2' doesn't match value '0' in mocs_compilation.cpp.obj QICNSPlugin_init.cpp.obj : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MT_StaticRelease' in mocs_compilation.cpp.obj QWindowsVistaStylePlugin_init.cpp.obj : warning LNK4099: PDB 'vc140.pdb' was not found with 'QWindowsVistaStylePlugin_init.cpp.obj' or at 'vc140.pdb'; linking object as if no debug info QICOPlugin_init.cpp.obj : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '2' doesn't match value '0' in mocs_compilation.cpp.obj QICOPlugin_init.cpp.obj : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MT_StaticRelease' in mocs_compilation.cpp.obj QGifPlugin_init.cpp.obj : warning LNK4099: PDB 'vc140.pdb' was not found with 'QGifPlugin_init.cpp.obj' or at 'vc140.pdb'; linking object as if no debug info QJpegPlugin_init.cpp.obj : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '2' doesn't match value '0' in mocs_compilation.cpp.obj QJpegPlugin_init.cpp.obj : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MT_StaticRelease' in mocs_compilation.cpp.obj QICNSPlugin_init.cpp.obj : warning LNK4099: PDB 'vc140.pdb' was not found with 'QICNSPlugin_init.cpp.obj' or at 'vc140.pdb'; linking object as if no debug info QSvgIconPlugin_init.cpp.obj : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '2' doesn't match value '0' in mocs_compilation.cpp.obj QSvgIconPlugin_init.cpp.obj : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MT_StaticRelease' in mocs_compilation.cpp.obj QICOPlugin_init.cpp.obj : warning LNK4099: PDB 'vc140.pdb' was not found with 'QICOPlugin_init.cpp.obj' or at 'vc140.pdb'; linking object as if no debug info QJpegPlugin_init.cpp.obj : warning LNK4099: PDB 'vc140.pdb' was not found with 'QJpegPlugin_init.cpp.obj' or at 'vc140.pdb'; linking object as if no debug info QSvgPlugin_init.cpp.obj : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '2' doesn't match value '0' in mocs_compilation.cpp.obj QSvgPlugin_init.cpp.obj : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MT_StaticRelease' in mocs_compilation.cpp.obj QTgaPlugin_init.cpp.obj : error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '2' doesn't match value '0' in mocs_compilation.cpp.obj QTgaPlugin_init.cpp.obj : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MTd_StaticDebug' doesn't match value 'MT_StaticRelease' in mocs_compilation.cpp.obj
My Qt build configuration:
-static -static-runtime -debug-and-release -mp -opensource -confirm-license -qt-zlib -qt-pcre -qt-libpng -qt-libjpeg -qt-freetype -no-tiff
Qt Version: 6.2.4
VS Environemnt: Visual Studion 17 2022
Platform: Windows 10 -
@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.
wrote on 8 Sept 2022, 17:08 last edited by DerReisende 9 Aug 2022, 17:09@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.
-
wrote on 8 Sept 2022, 09:17 last edited by
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") -
wrote on 8 Sept 2022, 09:41 last edited by
@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 )
-
wrote on 8 Sept 2022, 10:47 last edited by
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). -
wrote on 8 Sept 2022, 16:33 last edited by
@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.
-
@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.
wrote on 8 Sept 2022, 17:08 last edited by DerReisende 9 Aug 2022, 17:09@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.
-
wrote on 9 Sept 2022, 07:57 last edited by
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?
-
wrote on 9 Sept 2022, 08:27 last edited by
@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
-
@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
wrote on 9 Sept 2022, 09:23 last edited by@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. -
@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
wrote on 9 Sept 2022, 11:37 last edited by@Michael418 You cannot avoid dependencies to windows system dlls.
There is a reason why qt has a minimum OS requirement. -
wrote on 9 Sept 2022, 12:14 last edited by
@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.
-
@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.
wrote on 9 Sept 2022, 12:56 last edited by JonB 9 Sept 2022, 12:57@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/12