Skip to content

Commit 17c7f45

Browse files
Warn if MSVC Debug build uses wrong runtime
Check if CMAKE_MSVC_RUNTIME_LIBRARY != MultiThreadedDLL and emit a warning otherwise
1 parent 1906235 commit 17c7f45

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

cmake/CxxQt.cmake

+20
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#
55
# SPDX-License-Identifier: MIT OR Apache-2.0
66

7+
option(CXX_QT_SUPPRESS_MSVC_RUNTIME_WARNING "Disable checking that the CMAKE_MSVC_RUNTIME_LIBRARY is set when importing Cargo targets in Debug builds with MSVC.")
8+
79
find_package(Corrosion QUIET)
810
if(NOT Corrosion_FOUND)
911
include(FetchContent)
@@ -43,6 +45,24 @@ function(cxx_qt_import_crate)
4345
message(VERBOSE "CXX_QT_QT_MODULES: ${IMPORT_CRATE_QT_MODULES}")
4446
endif()
4547

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+
4666
foreach(CRATE ${__cxx_qt_imported_crates})
4767
# Join modules by a comma so that we can pass easily via an env variable
4868
#

0 commit comments

Comments
 (0)