[Interest] [Qt6] QML automatic type registration in a (shared) library
Konstantin Shegunov
kshegunov at gmail.com
Fri Jan 14 22:30:25 CET 2022
Well, I did find out what the problem is, and how to work around it. So I'm
posting for further reference to anyone that may stumble onto this.
On Fri, Jan 14, 2022 at 8:09 AM Ulf Hermann <ulf.hermann at qt.io> wrote:
> No. You can store a pointer to the type registration function somewhere
> in order to prevent the linker from dropping the dependency.
Assuming I understood the documentation correctly - yes, but no, not
exactly anyway. After inspecting the symbols, they (in my case 'it') are
exported just fine, the library itself isn't stripped or anything.
The problem occurs because this object:
static const QQmlModuleRegistration registration("Tsc.Ui",
qml_register_types_Tsc_Ui);
isn't created at all. So this prompted me to dig a bit deeper, into the
application binary this time. It turns out that the linker in question
(msvc19) checks if you refer to symbols from external binaries explicitly,
and if you don't it silently drops the dependency on the library.
Thereafter the library isn't loaded at all, the static object isn't
initialized, hence the registration code isn't run at all. Which also
explains why calling it explicitly does work. In my case to make extra sure
I exported a dummy global symbol from the dll. If I don't do anything with
it - nothing works. If I print it with qDebug() in the application code,
the library is loaded and registration code and all works just as expected.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20220114/192da12b/attachment.htm>
More information about the Interest
mailing list