[Interest] What is the right way of creating a static lib that has qml resources with cmake?

Nuno Santos nuno.santos at imaginando.pt
Wed Jan 11 17:36:35 CET 2023


Ulf,

In my latest try I give it a shot to the NO_PLUGIN approach and removed the qt_add_library line. I have also not set qml files with qt_add_resources

qt_add_qml_module(shared STATIC
    URI shared
    VERSION 1.0
    QML_FILES ${SHARED_QML_FILES}
    SOURCES ${SHARED_SOURCE_FILES}
    NO_PLUGIN
    #NO_LINT
    #NO_CACHEGEN
)
#set_source_files_properties(<qml files> PROPERTIES BASE <base>)
qt_add_resources(shared "shared_fonts" PREFIX "/" BASE resources FILES ${FONT_FILES})
qt_add_resources(shared "shared_icons" PREFIX "/" BASE resources FILES ${ICON_FILES})
qt_add_resources(shared "shared_js" PREFIX "/" BASE resources FILES ${JS_FILES})
qt_add_resources(shared "shared_json" PREFIX "/" BASE resources FILES ${JSON_FILES})
qt_add_resources(shared "shared_svg" PREFIX "/" BASE resources FILES ${SVG_FILES})


But in the end, the only file that contains qInitResources is: libshared.dylib

But I want it to be libshared.a

What am I missing?

Thanks!

Best,

Nuno

> On 11 Jan 2023, at 16:19, Nuno Santos <nuno.santos at imaginando.pt> wrote:
> 
> Ulf,
> 
> Thanks for your reply. So that you better understand my problem this is what I have
> 
> shared
> 	CMakeList.txt
> 
> project
> 	CMakeList.txt
> 	common
> 		CMakeList.txt
> 
> 
> In order to build project I need shared and common. When in debug I don’t want to build shared and common individually for each change so in project CMakeList.txt I do this:
> 
> if(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
> 	add_subdirectory(common)
> 	target_link_libraries(bam PRIVATE shared)
> 	target_link_libraries(bam PRIVATE common)
> endif()
> 
> When doing this, all the qml resources are found and the application works as expected
> 
> In release I just want to link with shared and common so I have something like this:
> 
> if(${CMAKE_BUILD_TYPE} STREQUAL “Release")
> 	target_link_libraries(bam PRIVATE libshared.a)
> 	target_link_libraries(bam PRIVATE libcommon.a)
> 	target_link_libraries(bam PRIVATE -L${CMAKE_CURRENT_SOURCE_DIR}/common/build/lib)
> 	target_include_directories(bam PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/common/build/include)
> endif()
> 
> The problem is that when linking with libshared.a and libcommon.a the resources are not available and when I try to call the function:
> 
> Q_INIT_RESOURCE(shared_qml);
> 
> It says:
> 
> Undefined symbols for architecture arm64:
>   "qInitResources_shared_qml()", referenced from:
> 
> The function is not available but searching for that function in binaries says:
> 
> Binary file shared/libshared.dylib matches
> 
> I’m linking (and I want to link) with the static version of this lib and not the dynamic one.
> 
> I think don’t totally understand your suggestion. My goal is to have a single libshared.a that I can link with my project and call the necessary functions to register the resources. 
> 
> What would be the right solution for this case?
> 
> Thanks in advance!
> 
> Regards,
> 
> Nuno
> 
> 
>> On 11 Jan 2023, at 15:43, Ulf Hermann via Interest <interest at qt-project.org> wrote:
>> 
>> 
>>> With the following approach two libs are created:
>>> 
>>> libshared.a and libsharedplugin.a
>>> 
>>> I just want to have a single target called libshared.a with all the qml resources embedded.
>> 
>> You can add NO_PLUGIN to your qt_add_qml_module, but be warned: The linker will be clever and omit your type registrations. You have to prevent that in some other way then. The usual way to prevent it is Q_IMPORT_PLUGIN on the generated plugin.
>> 
>> And you should not add your QML files with qt_add_resources. The most important thing qt_add_qml_module does is adding your QML files.
>> 
>> best regards,
>> 
>> Ulf
>> 
>> _______________________________________________
>> Interest mailing list
>> Interest at qt-project.org
>> https://lists.qt-project.org/listinfo/interest
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20230111/213a9bee/attachment.htm>


More information about the Interest mailing list