From e343affd6345ef8db041789a96016c3a84830dc9 Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Fri, 2 Aug 2019 12:01:00 +0200 Subject: [PATCH] Propagate library/plugin resources automatically when linking statically The rcc generated code relies on global constructors to register the resources. The object file of the generated code is included by default in shared libraries and executables. However when the object file ends up in a static library, the linker will discard the object file when nothing references any of the symbols in that object file, when linking the static library into the executable/shared library. The solution is to link the object file straight into the final target, by means of a cmake object library. Change-Id: I02ad1173e4f7e8f907022c906640dc9086233676 Reviewed-by: Alexandru Croitor --- cmake/QtBuild.cmake | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/cmake/QtBuild.cmake b/cmake/QtBuild.cmake index 3e2d406d9b..e893ac26ba 100644 --- a/cmake/QtBuild.cmake +++ b/cmake/QtBuild.cmake @@ -2570,7 +2570,20 @@ function(add_qt_resource target resourceName) DEPENDS ${files} COMMENT "RCC ${resourceName}" VERBATIM) - target_sources(${target} PRIVATE "${generatedSourceCode}") + + get_target_property(type "${target}" TYPE) + if(type STREQUAL STATIC_LIBRARY) + set(resourceTarget "${target}_resources_${resourceName}") + add_library("${resourceTarget}" OBJECT "${generatedSourceCode}") + qt_internal_add_target_aliases("${resourceTarget}") + qt_install(TARGETS "${resourceTarget}" + EXPORT "${INSTALL_CMAKE_NAMESPACE}${target}Targets" + DESTINATION ${INSTALL_LIBDIR} + ) + target_link_libraries(${target} INTERFACE "$") + else() + target_sources(${target} PRIVATE "${generatedSourceCode}") + endif() endfunction()