CMake: Use correct MSVC runtime library for Qt object libraries

If Qt was configured with -static-runtime which implies MultiThreaded
-MT flag, the plugin initializer object libraries were still compiled
with the default -MD flag.

When an application linked to Qt, that caused linking to fail with
mismatched symbol errors between the application symbols and the
plugin initializer object library symbols.

Make sure to set the MSVC_RUNTIME_LIBRARY property on both plugin
initializer and resource object libraries, depending on the value of
QT_FEATURE_static_runtime.

We did set the property for resources added by
qt_internal_add_resource, but not for the resource created by
the public qt6_add_resources counterpart.

Pick-to: 6.2
Fixes: QTBUG-95043
Change-Id: Ia543cd0241db94a12080be2655ad420fe9ad3f24
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Joerg Bornemann <joerg.bornemann@qt.io>
This commit is contained in:
Alexandru Croitor 2021-07-08 14:01:59 +02:00
parent 7b3d9efc04
commit 51ae235080
4 changed files with 14 additions and 8 deletions

View File

@ -243,6 +243,7 @@ function(__qt_internal_add_static_plugin_init_object_library
# Core provides the symbols needed by Q_IMPORT_PLUGIN.
${QT_CMAKE_EXPORT_NAMESPACE}::Core
)
_qt_internal_set_up_static_runtime_library("${plugin_init_target}")
set_property(TARGET ${plugin_init_target} PROPERTY _is_qt_plugin_init_target TRUE)
set_property(TARGET ${plugin_init_target} APPEND PROPERTY

View File

@ -299,3 +299,14 @@ function(__qt_internal_defer_promote_targets_in_dir_scope_to_global)
endif()
endif()
endfunction()
function(_qt_internal_set_up_static_runtime_library target)
if(QT_FEATURE_static_runtime)
if(MSVC)
set_property(TARGET ${target} PROPERTY
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
elseif(MINGW)
target_link_options(${target} INTERFACE "LINKER:-static")
endif()
endif()
endfunction()

View File

@ -188,14 +188,7 @@ function(qt_set_common_target_properties target)
OBJCXX_VISIBILITY_PRESET hidden
VISIBILITY_INLINES_HIDDEN 1)
endif()
if(QT_FEATURE_static_runtime)
if(MSVC)
set_property(TARGET ${target} PROPERTY
MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
elseif(MINGW)
target_link_options(${target} INTERFACE "LINKER:-static")
endif()
endif()
_qt_internal_set_up_static_runtime_library("${target}")
qt_internal_set_compile_pdb_names("${target}")
endfunction()

View File

@ -1587,6 +1587,7 @@ function(__qt_propagate_generated_resource target resource_name generated_source
target_compile_definitions("${resource_target}" PRIVATE
"$<TARGET_PROPERTY:${QT_CMAKE_EXPORT_NAMESPACE}::Core,INTERFACE_COMPILE_DEFINITIONS>"
)
_qt_internal_set_up_static_runtime_library("${resource_target}")
# Special handling is required for the Core library resources. The linking of the Core
# library to the resources adds a circular dependency. This leads to the wrong