Link errors when using same class in both a QML module and a library
-
Hi,
For my project I want to have a library and a QML module both using the same class and then using both the lib and the module in the same application.
The class I use in this example is CUIScene3D which inherited from QObject and as Q_OBJECT and QML_ELEMENT macros.
--
I have a 3D lib, defined using:qt_add_library(Lib_3D STATIC UIScene3D.cpp )
The MOC will generate a mocs_compilation_Release.cpp file.
--
I have a QML module, defined using:qt_add_qml_module(Qml_DDD URI QmlModules.DDD VERSION 1.0 QML_FILES MeshScene.qml SOURCES ../../Libs/3D/UIScene3D.h )
I added the UIScene3D.h because I use QML_ELEMENT to add the QML types in the module.
The MOC will generate a mocs_compilation_Release.cpp file.
Both mocs_compilation_Release.cpp files include a moc_CUIScene3D.cpp which has the same content.
--
Then I finally have an executable that link to both the lib and the module using:target_link_libraries(MyExe PRIVATE Lib_3D Qml_DDDplugin)
In main.cpp I use the Q_IMPORT_QML_PLUGIN macro
Q_IMPORT_QML_PLUGIN(QmlModules_DDDPlugin)
During the link, I have the following errors:
Lib_3D.lib(mocs_compilation_Release.cpp.obj):-1: error: LNK2005: "private: static void __cdecl CUIScene3D::qt_static_metacall(class QObject *,enum QMetaObject::Call,int,void * *)" (?qt_static_metacall@CUIScene3D@@CAXPEAVQObject@@W4Call@QMetaObject@@HPEAPEAX@Z) already defined in Qml_DDD.lib(mocs_compilation_Release.cpp.obj) Lib_3D.lib(mocs_compilation_Release.cpp.obj):-1: error: LNK2005: "public: virtual int __cdecl CUIScene3D::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@CUIScene3D@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z) already defined in Qml_DDD.lib(mocs_compilation_Release.cpp.obj) Lib_3D.lib(mocs_compilation_Release.cpp.obj):-1: error: LNK2005: "public: virtual struct QMetaObject const * __cdecl CUIScene3D::metaObject(void)const " (?metaObject@CUIScene3D@@UEBAPEBUQMetaObject@@XZ) already defined in Qml_DDD.lib(mocs_compilation_Release.cpp.obj) Lib_3D.lib(mocs_compilation_Release.cpp.obj):-1: error: LNK2005: "public: virtual void * __cdecl CUISimpleScene3D::qt_metacast(char const *)" (?qt_metacast@CUISimpleScene3D@@UEAAPEAXPEBD@Z) already defined in Qml_DDD.lib(mocs_compilation_Release.cpp.obj) Lib_3D.lib(mocs_compilation_Release.cpp.obj):-1: error: LNK2005: "public: static struct QMetaObject const CUIScene3D::staticMetaObject" (?staticMetaObject@CUIScene3D@@2UQMetaObject@@B) already defined in Qml_DDD.lib(mocs_compilation_Release.cpp.obj)
and the same error for all the signals declared in CUIScene3D.h.
I think the message is correct but what I don't understand is that I have done the same thing for several other libs/modules without any link issue and I don't know how to explain it, any ideas?
I use ninja to generate the build files and MSVC to build.
--
I also tried to build an executable using two libs using:target_link_libraries(MyExe PRIVATE Lib_A Lib_B)
with Lib_B a duplicate of Lib_A (in the lib I include CUIScene3D.cpp), in main.cpp I instantiate the CUIScene3D class.
In this case it links without any issue but the symbols defined in the libs are duplicated are they not?