Skip to content

Commit 0f25fd0

Browse files
Add support for the debug runtime in MSVC builds.
We can force the linker to choose the debug runtime, which is necessary to override rusts libc, which by default wants to link to the release runtime, even in debug mode. With this patch CMake will override this behavior and use the debug runtime if appropriate. This is very important, as all parts of the program must agree on the runtime! As Qt in Debug mode uses the debug runtime in their DLLs, we may encounter undefined behavior otherwise.
1 parent 288779e commit 0f25fd0

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

cmake/CxxQt.cmake

+26-18
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,6 @@ function(cxx_qt_import_crate)
4545
message(VERBOSE "CXX_QT_QT_MODULES: ${IMPORT_CRATE_QT_MODULES}")
4646
endif()
4747

48-
if ((NOT CXX_QT_SUPPRESS_MSVC_RUNTIME_WARNING)
49-
AND (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
50-
AND (CMAKE_BUILD_TYPE STREQUAL "Debug")
51-
AND (NOT (CMAKE_MSVC_RUNTIME_LIBRARY STREQUAL "MultiThreadedDLL")))
52-
message(WARNING
53-
" CXX-Qt Warning: CMAKE_MSVC_RUNTIME_LIBRARY not set in MSVC Debug build!\n \n"
54-
" To fix this, set CMAKE_MSVC_RUNTIME_LIBRARY=\"MultiThreadedDLL\" when configuring.\n \n"
55-
" When building with MSVC in Debug, the CMAKE_MSVC_RUNTIME_LIBRARY variable should be set to \"MultiThreadedDLL\"\n"
56-
" This needs to be done before configuring any target that links to a Rust target.\n"
57-
" Otherwise, you may encounter linker errors when linking to Rust targets, like:\n \n"
58-
" error LNK2038: mismatch detected for '_ITERATOR_DEBUG_LEVEL': value '0' doesn't match value '2' in ...\n \n"
59-
" See also:\n"
60-
" https://github.com/corrosion-rs/corrosion/blob/master/doc/src/common_issues.md#linking-debug-cc-libraries-into-rust-fails-on-windows-msvc-targets\n"
61-
" and: https://github.com/KDAB/cxx-qt/pull/683\n \n"
62-
" To suppress this warning set CXX_QT_SUPPRESS_MSVC_RUNTIME_WARNING to ON"
63-
)
64-
endif()
65-
6648
foreach(CRATE ${__cxx_qt_imported_crates})
6749
# Join modules by a comma so that we can pass easily via an env variable
6850
#
@@ -128,8 +110,34 @@ function(cxx_qt_import_crate)
128110
# This can cause CMake to emit the wrong link order, with Qt before the static library, which then fails to build with ld.bfd
129111
# https://stackoverflow.com/questions/51333069/how-do-the-library-selection-rules-differ-between-gold-and-the-standard-bfd-li
130112
target_link_libraries(${CRATE}-static INTERFACE ${IMPORT_CRATE_QT_MODULES})
113+
114+
if ((CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
115+
AND (CMAKE_BUILD_TYPE STREQUAL "Debug")
116+
AND (NOT (CMAKE_MSVC_RUNTIME_LIBRARY STREQUAL "MultiThreadedDLL")))
117+
# MSVC(Debug): Tell the linker not to link to the MultiThreadedDLL runtime and use the Debug version instead
118+
# This is a new workaround for this issue:
119+
# https://corrosion-rs.github.io/corrosion/common_issues.html#linking-debug-cc-libraries-into-rust-fails-on-windows-msvc-targets
120+
# As outlined in this comment:
121+
# https://github.com/rust-lang/rust/issues/39016#issuecomment-2521395154
122+
target_link_options(${CRATE}-static INTERFACE /NODEFAULTLIB:msvcrt /DEFAULTLIB:msvcrtd)
123+
endif()
131124
endforeach()
132125

126+
if((NOT CXX_QT_SUPPRESS_MSVC_RUNTIME_WARNING)
127+
AND (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
128+
AND (CMAKE_BUILD_TYPE STREQUAL "Debug")
129+
AND (CMAKE_MSVC_RUNTIME_LIBRARY STREQUAL "MultiThreadedDLL"))
130+
message(WARNING
131+
" CXX-Qt Warning: CMAKE_MSVC_RUNTIME_LIBRARY should no longer be set in MSVC Debug build!\n \n"
132+
" In previous versions of CXX-Qt it was necessary to set CMAKE_MSVC_RUNTIME_LIBRARY=\"MultiThreadedDLL\".\n"
133+
" Starting with CXX-Qt 0.7.2, this has been fixed and is no longer necessary or recommended.\n \n"
134+
135+
" See also:\n"
136+
" https://github.com/KDAB/cxx-qt/issues/1234\n \n"
137+
" To suppress this warning set CXX_QT_SUPPRESS_MSVC_RUNTIME_WARNING to ON"
138+
)
139+
endif()
140+
133141
endfunction()
134142

135143

0 commit comments

Comments
 (0)