Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

"Segmentation fault (core dumped)" error after running executable file on the Android device (using Android Clang (C++,aarch64) compiler)



  • Hi, I have a simple Qt code (main.cpp) as follow which I want to run in my Android device:

    #include <QCoreApplication>
    #include <QDebug>
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        qDebug()<<"The program starts successfully.";
        return a.exec();
    }
    

    1- At first, I have put this code within a Qt console project and I have used qmake to create Makefile. After compiling, Qt just outputs *.apk and *.so files from my project but I need executable output. Here is my project *.pro:

    QT -= gui
    
    CONFIG += c++11 console
    CONFIG -= app_bundle
    
    TEMPLATE = app
    DEFINES += QT_DEPRECATED_WARNINGS
    
    SOURCES += \
            main.cpp
    
    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target
    

    and I have:

    • Java ver 1.8.0_242: /usr/lib/jvm/java-8-openjdk-amd64

    • SDK ver 25.2.5: /home/***/Android/Android/Sdk

    • NDK ver 20.0.5594570: /home/***/Android/Android/Sdk/ndk-bundle

    2- In order to produce executable file, I have used Android Clang (C++,aarch64) compiler in a ubuntu terminal by running this command:

    /home/***/Android/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ \
    -D__ANDROID_API__=21 \
    -target aarch64-none-linux-android \
    -gcc-toolchain /home/***/Android/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64 \
    -fno-limit-debug-info \
    -DANDROID_HAS_WSTRING \
    --sysroot=/home/***/Android/Android/Sdk/ndk-bundle/platforms/android-21/arch-arm64 \
    -isystem /home/***/Android/Android/Sdk/ndk-bundle/sysroot/usr/include/aarch64-linux-android \
    -isystem /home/***/Android/Android/Sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/include \
    -isystem /home/***/Android/Android/Sdk/ndk-bundle/sources/android/support/include \
    -isystem /home/***/Android/Android/Sdk/ndk-bundle/sources/cxx-stl/llvm-libc++abi/include \
    -fstack-protector-strong -DANDROID -g -g -std=gnu++11 -Wall -W -D_REENTRANT -fPIC -DQT_DEPRECATED_WARNINGS -DQT_QML_DEBUG -DQT_CORE_LIB \
    -I/home/***/Qt5.12.7/5.12.7/android_arm64_v8a/mkspecs/android-clang \
    -I/home/***/Qt5.12.7/5.12.7/android_arm64_v8a/include \
    -I/home/***/Qt5.12.7/5.12.7/android_arm64_v8a/include/QtCore \
    -I/home/***/Android/Android/Sdk/ndk-bundle/sources/cxx-stl/llvm-libc++/include \
    -I/home/***/Android/Android/Sdk/ndk-bundle/sysroot/usr/include \
    -L/home/***/Android/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-android/21 \
    -L/home/***/Android/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/aarch64-linux-android \
    -L/home/***/Qt5.12.7/5.12.7/android_arm64_v8a/lib \
    main.cpp \
    -lc++ -lc++_shared -lQt5Core \
    -o executableMain 
    

    I have used some of the flags and include directories from Makefile in Step 1. After compiling, I can procude an executable file (executableMain). And also here is the results of file executableMain and file libc++_shared.so and file libQt5Core.so respectively:

    executableMain: ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, interpreter /system/, with debug_info, not stripped
    libc++_shared.so: ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, BuildID[sha1]=a9c0aad7747976d255d9ea273f6f4c5003901317, stripped
    libQt5Core.so: ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, for GNU/Linux 2.6.28, with debug_info, not stripped
    

    3- Now, I want to run the executableMain file in my Android device using adb shell. So, I have copied libc++_shared.so and libQt5Core.so to /System/lib64/ of my Android device and executableMain to a subfolder of /data/user/0. But after setting permission to executableMain (chmod 777 executableMain), running ./executableMain shows the following error:

    Segmentation fault (core dumped)
    

    How can I solve that?


  • Qt Champions 2019

    @morteza-ali-ahmadi The APK contains your executable. You can simply deploy it through QtCreator to your device and start.
    "How can I solve that?" - use debugger to see where it crashes and how stack trace looks like (you can post it here).



  • @jsulm I can simply deploy the apk app through QtCreator to my device and start but I want to get the executable output (in Linux without any extension and in windows with *.exe extension). Then I want to run the executable file from adb shell. Because finally, my executable file will be started from another android apk as a external process. So I need a executable format.

    Where can I use debugger? (Because this error is appeared on adb shell of my Android device)


  • Qt Champions 2019

    @morteza-ali-ahmadi said in "Segmentation fault (core dumped)" error after running executable file on the Android device (using Android Clang (C++,aarch64) compiler):

    Where can I use debugger?

    In QtCreator.

    You can open APK (it is an archive) and extract the executable from there. Keep in mind there will not be any.exe extension as it does not have anything to do with Windows.



  • @jsulm Thanks, I can extract it but there are some *.so files in lib folder (and also there are some folders like assets, META-INF and ...) and there is no executable file.



  • @jsulm Can you help me?


  • Qt Champions 2019

    @morteza-ali-ahmadi I'm not an Android expert. Maybe somebody else knows more.