Using Qt codes in Android application

174 views
Skip to first unread message

morteza ali Ahmadi

unread,
Mar 19, 2020, 6:32:26 PM3/19/20
to android-ndk
I have an Android application in Android Studio and I want to use Qt C++ codes in my app. I have used pure C++ codes in my android app using cmake and NDK recently and I could interact with my pure C++ and now I want to use Qt C++ codes.

To do this, I have installed Qt with Android capability. After that I copied Qt5.12.7/5.12.7/android_arm64_v8a/lib/*.so and includes file to my Android App /etc/ folder and I created a simple main.cpp and CMakeLists.txt files as follow:

main.cpp

    #include <QString>
    #include <QDebug>
    int main(int argc, char *argv[])
    {
        QString test="HELLO WORLD!";
        qDebug()<<test;
        return 0;
    }

CMakeLists.txt

    cmake_minimum_required(VERSION 3.4.1)
    include_directories(etc/headerFile)
    link_directories(etc/libs)
    add_library(po-lib SHARED main.cpp)
    target_link_libraries(po-lib -lQt5Core)

As seen in the above files, I want to compile a simple Qt C++ code with dependency on libQtCore.so in my Android App, but after compiling the App, the following errors occur:

    Build command failed.
    Error while executing process C:\Users\User1\AppData\Local\Android\Sdk\cmake\3.10.2.4988404\bin\ninja.exe with arguments {-C D:\WorkProjects\Horizon\Ga\po\.cxx\cmake\debug\armeabi-v7a po-lib}
    ninja: Entering directory `D:\WorkProjects\Horizon\Ga\po\.cxx\cmake\debug\armeabi-v7a'
    [1/2] Building CXX object CMakeFiles/po-lib.dir/main.cpp.o
    [2/2] Linking CXX shared library D:\WorkProjects\Horizon\Ga\po\build\intermediates\cmake\debug\obj\armeabi-v7a\libpo-lib.so
    FAILED: D:/WorkProjects/Horizon/Ga/po/build/intermediates/cmake/debug/obj/armeabi-v7a/libpo-lib.so
    cmd.exe /C "cd . && C:\Users\User1\AppData\Local\Android\Sdk\ndk\21.0.6113669\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=armv7-none-linux-androideabi21 --gcc-toolchain=C:/Users/User1/AppData/Local/Android/Sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/windows-x86_64 --sysroot=C:/Users/User1/AppData/Local/Android/Sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/windows-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=armv7-a -mthumb -Wformat -Werror=format-security   -O0 -fno-limit-debug-info  -Wl,--exclude-libs,libgcc_real.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--fatal-warnings -Wl,--exclude-libs,libunwind.a -Wl,--no-undefined -Qunused-arguments -shared -Wl,-soname,libpo-lib.so -o D:\WorkProjects\Horizon\Ga\po\build\intermediates\cmake\debug\obj\armeabi-v7a\libpo-lib.so CMakeFiles/po-lib.dir/main.cpp.o -LD:/WorkProjects/Horizon/Ga/po/src/main/cpp/etc/libs_a  -LD:/WorkProjects/Horizon/Ga/po/src/main/cpp/etc/libs_so -lQt5Core -latomic -lm && cd ."
    C:/Users/User1/AppData/Local/Android/Sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/windows-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin\ld: warning: skipping incompatible D:/WorkProjects/Horizon/Ga/po/src/main/cpp/etc/libs_so/libQt5Core.so while searching for Qt5Core
    C:/Users/User1/AppData/Local/Android/Sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/windows-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin\ld: error: cannot find -lQt5Core
    D:/WorkProjects/Horizon/Ga/po/src/main/cpp/main.cpp:25: error: undefined reference to 'QMessageLogger::debug() const'
    D:/WorkProjects/Horizon/Ga/po/src/main/cpp/main.cpp:25: error: undefined reference to 'QDebug::~QDebug()'
    D:/WorkProjects/Horizon/Ga/po/src/main/cpp/main.cpp:25: error: undefined reference to 'QDebug::~QDebug()'
    D:/WorkProjects/Horizon/Ga/po/src/main/cpp/etc/headerFile\QtCore/qstring.h:700: error: undefined reference to 'QString::fromAscii_helper(char const*, int)'
    D:/WorkProjects/Horizon/Ga/po/src/main/cpp/etc/headerFile\QtCore/qdebug.h:155: error: undefined reference to 'QDebug::putString(QChar const*, unsigned int)'
    D:/WorkProjects/Horizon/Ga/po/src/main/cpp/etc/headerFile\QtCore/qdebug.h:125: error: undefined reference to 'QTextStream::operator<<(char)'
    D:/WorkProjects/Horizon/Ga/po/src/main/cpp/etc/headerFile\QtCore/qarraydata.h:59: error: undefined reference to 'qt_assert(char const*, char const*, int)'
    D:/WorkProjects/Horizon/Ga/po/src/main/cpp/etc/headerFile\QtCore/qarraydata.h:239: error: undefined reference to 'QArrayData::deallocate(QArrayData*, unsigned int, unsigned int)'
    clang++: error: linker command failed with exit code 1 (use -v to see invocation)
    ninja: build stopped: subcommand failed.

I have used both arm64_v8a and armv7 libs in Qt folders but the same errors appear. How can I solve this linker error (incompatible lib)?

georg...@trimble.com

unread,
Mar 19, 2020, 8:38:50 PM3/19/20
to android-ndk
What is your build toolchain? Are you building through QtCreator?

Andrew Esh

unread,
Mar 20, 2020, 8:19:05 AM3/20/20
to android-ndk
It appears that D:/WorkProjects/Horizon/Ga/po/src/main/cpp/etc/libs_so/libQt5Core.so is not "-march=armv7-a" which is the machine type that the compiler is looking for. You said you "copied Qt5.12.7/5.12.7/android_arm64_v8a/lib/*" in the beginning of your message. That is not the same architecture. You need an armv7 version of libQt5Core.so.

morteza ali Ahmadi

unread,
Mar 20, 2020, 11:47:17 AM3/20/20
to android-ndk
I use NDK build toolchain and I don't use QtCreator for building.

morteza ali Ahmadi

unread,
Mar 20, 2020, 5:26:30 PM3/20/20
to android-ndk
Thanks, but I have tested for armv7 (as I mentioned in the last line of my message), i.e. I have copied both Qt5.12.7/5.12.7/android_arm64_v8a/lib/*.so and Qt5.12.7/5.12.7/android_armv7/lib/*.so to my application and the similar errors appear for both libs architecture (Sorry, the errors in my first message are appeared after using armv7 libs not arm64_v8a). I also built Qt sources for arm64_v8a architecture in Ubuntu and I copied the arm64_v8a libs to my android application but the similar errors appeared as follows:

Build command failed.
Error while executing process /home/user1/Documents/Programs/Android/Sdk/cmake/3.10.2.4988404/bin/ninja with arguments {-C /home/user1/Downloads/PolarisLib/app/.cxx/cmake/debug/arm64-v8a polaris-lib}
ninja: Entering directory `/home/user1/Downloads/PolarisLib/app/.cxx/cmake/debug/arm64-v8a'
[1/2] Building CXX object CMakeFiles/polaris-lib.dir/main.cpp.o
[2/2] Linking CXX shared library /home/user1/Downloads/PolarisLib/app/build/intermediates/cmake/debug/obj/arm64-v8a/libpolaris-lib.so
FAILED: /home/user1/Downloads/PolarisLib/app/build/intermediates/cmake/debug/obj/arm64-v8a/libpolaris-lib.so
: && /home/user1/Documents/Programs/Android/Sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=aarch64-none-linux-android21 --gcc-toolchain=/home/user1/Documents/Programs/Android/Sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/linux-x86_64 --sysroot=/home/user1/Documents/Programs/Android/Sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/linux-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security   -O0 -fno-limit-debug-info  -Wl,--exclude-libs,libgcc_real.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments -shared -Wl,-soname,libpolaris-lib.so -o /home/user1/Downloads/PolarisLib/app/build/intermediates/cmake/debug/obj/arm64-v8a/libpolaris-lib.so CMakeFiles/polaris-lib.dir/main.cpp.o -L/home/user1/Downloads/PolarisLib/app/src/main/cpp/etc/lib -lQt5Core -latomic -lm && :
/home/user1/Documents/Programs/Android/Sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/aarch64-linux-android/4.9.x/../../../../aarch64-linux-android/bin/ld: skipping incompatible /home/user1/Downloads/PolarisLib/app/src/main/cpp/etc/lib/libQt5Core.so when searching for -lQt5Core
/home/user1/Documents/Programs/Android/Sdk/ndk/21.0.6113669/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/aarch64-linux-android/4.9.x/../../../../aarch64-linux-android/bin/ld: cannot find -lQt5Core

clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

Andrew Esh

unread,
Mar 23, 2020, 8:22:17 AM3/23/20
to android-ndk
The target architecture has to match the architecture of the libraries you are linking with. You have "clang++ --target=aarch64-none-linux-android21", and "-march=armv7-a", and "D:/WorkProjects/Horizon/Ga/po/src/main/cpp/etc/libs_so/libQt5Core.so". I do not know which architecture the library is, but all three of these must match. The error message is saying they do not.
Reply all
Reply to author
Forward
0 new messages