Undefined reference to qInitResources



  • Hi,
    I am trying to port my C++ application to Android.
    Unfortunately, I got error messages like:

    C:\myGame\src\shaders/carMaterial.cpp:28: error: undefined reference to 'CarMaterial::qInitResources_materials()'
    

    Android project files are made from CMakeList - without resources everything works fine.
    My CMakeList contains these lines:

    (...)
    QT5_ADD_RESOURCES("./resources/qml.qrc")
    
    SET(ALL_SOURCES
        ${__main_HDRSRC}
        (...)
        ${__resources_HDRSRC}   
    )
    
    IF(ANDROID)
        ADD_LIBRARY(myGame SHARED ${ALL_SOURCES})
    ELSE()
        ADD_EXECUTABLE(myGame WIN32 ${ALL_SOURCES})
    ENDIF()
    
    (...)
    

    I used toolchain from https://github.com/LaurentGomila/qt-android-cmake
    And command to making project files:

    cmake -DCMAKE_CXX_COMPILE_FEATURES=cxx_constexpr -DCMAKE_CXX_FLAGS="-std=gnu++14" -D ANDROID_NATIVE_API_LEVEL=android-19 -D CMAKE_TOOLCHAIN_FILE=../../qt-android-cmake/toolchain/android.toolchain.cmake -D CMAKE_MAKE_PROGRAM="%ANDROID_NDK%/prebuilt/windows-x86_64/bin/make.exe" -D QTDIR="%QTDIR%" -G "MinGW Makefiles" ..
    

    Any idea how to solve this problem?

    #edit
    My OS: Windows 10


  • Moderators

    @Montjet said in Undefined reference to qInitResources:

    QT5_ADD_RESOURCES("./resources/qml.qrc")

    This doesn't look right... You need something like:

    qt5_add_resources(RC_SRC "resources/qml.qrc")
    ...
    add_library(myGame SHARED ${ALL_SOURCES} ${RC_SRC})
    

    That should fix your issue. Basically that error message is saying you didn't include your resources in the shared object file so they can't be loaded.



  • It doesn't work :(



  • Okay, I got it!
    I moved every Q_INIT_RESOURCE(...) outside my namespace.
    Now, it's linked and works but my android app is crashed, I think it's a different issue :{


  • Moderators

    @Montjet Oh sorry I should have mentioned that. You can't have a namespace around Q_INIT_RESOURCES.

    It's covered in the Qt docs on using resources in a shared object. I didn't think to mention it.

    The crash is more than likely completely unrelated, so we'll consider this solved for now. Glad at least 1 part of your problem is over. :)


Log in to reply
 

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