CMake link error resource file



  • Hi,

    I'm setting up my environment for a C++/Qt Quick application (Windows VS2013 x64) with cmake. I'm having trouble to get a (shared) library be compiled and linked to my executable and using a resource file from it (just a simple qml file to test the setup).

    I have set(CMAKE_AUTOMOC ON) and set(CMAKE_AUTORCC ON) in my root CMakeLists.txt
    The CMakeLists.txt of my shared library is as follows (BEQMLResource.qrc is in the same directory as CMakeLists.txt):

        cmake_minimum_required(VERSION 3.2)
         
        if(UNIX)
            SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=gnu++11")
        endif()
         
        SET(QT_ROOT_DIR "C:/Qt/5.7/msvc2013_64/")
         
        set(QT_QMAKE_EXECUTABLE ${QT_ROOT_DIR}/bin/qmake)
         
        find_package(Qt5 COMPONENTS Quick Core Network Widgets Gui Qml Positioning PATHS ${QT_ROOT_DIR})
         
        project("BEQML")
        set(PROJECT_RESOURCES BEQMLResource.qrc)
        qt5_add_resources(BEQML_RESOURCES ${PROJECT_RESOURCES})
        file(GLOB BEQML_SRC_LIBRARY
        	"BEQMLLibrary.h" #contains dll import/export defines for windows.
        	"BEQMLLibraryInitializer.cpp" #dummy class with some functions so the library is created.
        	"BEQMLLibraryInitializer.h"
        )
        source_group("Library"		FILES ${BEQML_SRC_LIBRARY})
        add_library(BEQML SHARED ${BEQML_SRC_LIBRARY} ${BEQML_RESOURCES})
        target_include_directories(BEQML PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../BELibraries/)
    

    When I compile the library, I get two linker errors in qrc_BEQMLResource.obje (unresolved external symbol for qRegisterResourceData and qUnregisterResourceData).
    Any idea what can be wrong?
    Kind regards,

    Jan


  • Moderators

    @JanW That indicates it isn't linking with Qt properly would be my guess.

    Try adding the line: qt5_use_modules(${PROJECT_NAME} Core) after your add_library line.



  • Thanks, that already solved my first problem! The BEQML library is building without errors, but now I got a link error in my executable project: unresolved external symbol qInitResource_BEQMLResource.
    The CMakeLists.txt from my exe project is as follows:

    cmake_minimum_required(VERSION 3.2 FATAL_ERROR)
    
    if(UNIX)
        SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=gnu++11")
    endif()
    
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
    
    SET(QT_ROOT_DIR "C:/Qt/5.7/msvc2013_64/")
    
    set(QT_QMAKE_EXECUTABLE ${QT_ROOT_DIR}/bin/qmake)
    
    find_package(Qt5 COMPONENTS Quick Core Network Widgets Gui Qml Positioning PATHS ${QT_ROOT_DIR})
    
    project("StormExe")
    
    qt5_add_resources(RESOURCES StormExe.qrc)
    
    file(GLOB STORMEXE_SRC_QML
    	"QML/Main.qml"
    )
    file(GLOB STORMEXE_SRC_EXE
    	"Main.cpp"
    )
    
    source_group("Exe"	FILES ${STORMEXE_SRC_EXE})
    source_group("QML"	FILES ${STORMEXE_SRC_QML})
    
    add_executable(Storm ${STORMEXE_SRC_EXE} ${RESOURCES} ${STORMEXE_SRC_QML})
    qt5_use_modules(Storm Quick Core Network Widgets Gui Qml Positioning)
    
    target_link_libraries(Storm BEQML)
    target_include_directories(Storm PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../../BELibraries/)
    

    In my Main.cpp i have:

    #include <QApplication>
    #include <QQmlApplicationEngine>
    
    int main(int argc, char *argv[])
    {
    	QApplication app(argc, argv);
    
    	Q_INIT_RESOURCE(BEQMLResource);
    
    	QQmlApplicationEngine engine;
    	engine.load(QUrl(QStringLiteral("qrc:/Main.qml")));
    
    	return app.exec();
    }
    

    Do I need to do something special to get access to the resources from the shared library?
    Regards,

    Jan

    Btw, if i build as a static lib, everything works just fine...



  • Ok, found a workaround by exporting a custom method from the dll which calls the initialisation of the resource like this:

    int BEQMLLibraryInitializer::init()
    {
    	extern int qInitResources_BEQMLResource();
    	return qInitResources_BEQMLResource();
    }
    

    Is there a better way to do it?
    Regards,

    Jan


  • Moderators

    @JanW This answers it better than I could:

    http://stackoverflow.com/questions/14633454/how-can-i-embed-a-qt-resource-into-a-dll-file

    Glad it's working for you now though. :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.