CMake: Fix examples that use 'shared' project when using a static Qt

When using a static Qt, linking of examples that use the 'shared' Qml
module would fail with the following error

Undefined symbols for architecture x86_64:
  "qt_static_plugin_sharedPlugin()", referenced from:
  StaticsharedPluginPluginInstance::StaticsharedPluginPluginInstance()
  in window_shared_init.cpp.o

This happened because the 'shared' project pre-created its
plugin target with qt_add_library instead of qt_add_plugin.

qt_add_plugin passes an additional QT_STATICPLUGIN compile definition
when compiling the moc'ed file to ensure that the QT_MOC_EXPORT_PLUGIN
macro creates a qt_plugin_instance_PLUGIN_NAME symbol.

Unfortunately we can't use qt_add_plugin for shared Qt builds, because
some of the projects link directly against the plugin target and it's
not possible to link against a MODULE_LIBRARY target which
qt_add_plugin creates in shared Qt build.

We could try to conditionally switch between using qt_add_library for
a shared Qt build and qt_add_plugin for a static Qt build, but that
further complicates the build system code because it requires
specifying a class name and plugin type explicitly.

Remove the direct linkage against the libraries in the examples and
instead rely on plugin loading.
This simplifies the logic of not having to pre-create a target.

Amends 7b6eea37ae

Pick-to: 6.2
Fixes: QTBUG-96805
Change-Id: I5b2f3992ccda29b59f1e99748005381c73daca69
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Alexandru Croitor 2021-09-23 16:55:11 +02:00
parent 2a04bfaf3a
commit edc4357ae4
9 changed files with 8 additions and 10 deletions

View File

@ -34,8 +34,8 @@ target_link_libraries(animationexample PUBLIC
Qt::Gui
Qt::Qml
Qt::Quick
animation_shared
)
add_dependencies(animationexample animation_shared)
qt_add_qml_module(animationexample
URI animation

View File

@ -26,8 +26,8 @@ target_link_libraries(delegatechooserexample PUBLIC
Qt::Gui
Qt::Qml
Qt::Quick
delegatechooser_shared
)
add_dependencies(delegatechooserexample delegatechooser_shared)
qt_add_qml_module(delegatechooserexample
URI delegatechooser

View File

@ -34,8 +34,8 @@ target_link_libraries(shapesexample PUBLIC
Qt::Gui
Qt::Qml
Qt::Quick
shapes_shared
)
add_dependencies(shapesexample delegatechooser_shared)
qt_add_qml_module(shapesexample
URI shapes

View File

@ -1,5 +1,3 @@
qt_add_library(${PROJECT_NAME}_shared)
set_source_files_properties(CheckBox.qml TabSet.qml TextField.qml
PROPERTIES
QT_QML_SOURCE_VERSIONS 2.1

View File

@ -34,8 +34,8 @@ target_link_libraries(textexample PUBLIC
Qt::Gui
Qt::Qml
Qt::Quick
text_shared
)
add_dependencies(textexample text_shared)
qt_add_qml_module(textexample
URI text

View File

@ -34,8 +34,8 @@ target_link_libraries(threadingexample PUBLIC
Qt::Gui
Qt::Qml
Qt::Quick
threading_shared
)
add_dependencies(threadingexample threading_shared)
qt_add_qml_module(threadingexample
URI threading

View File

@ -34,8 +34,8 @@ target_link_libraries(touchinteractionexample PUBLIC
Qt::Gui
Qt::Qml
Qt::Quick
touchinteraction_shared
)
add_dependencies(touchinteractionexample touchinteraction_shared)
qt_add_qml_module(touchinteractionexample
URI touchinteraction

View File

@ -34,8 +34,8 @@ target_link_libraries(viewsexample PRIVATE
Qt::Gui
Qt::Qml
Qt::Quick
views_shared
)
add_dependencies(viewsexample views_shared)
qt_add_qml_module(viewsexample
URI views

View File

@ -45,8 +45,8 @@ target_link_libraries(windowexample PRIVATE
Qt::Gui
Qt::Qml
Qt::Quick
window_shared
)
add_dependencies(windowexample window_shared)
install(TARGETS windowexample
RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"