Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Why the same C++ library has much smaller size in static build than shared build, and static is not working while shared works?
QtWS25 Last Chance

Why the same C++ library has much smaller size in static build than shared build, and static is not working while shared works?

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
3 Posts 2 Posters 525 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • advmA Offline
    advmA Offline
    advm
    wrote on last edited by
    #1

    Hello, I have problem when I try to use C++ library in Android studio project.

    I am using statically built Qt 5.14.2 for Android. I have Qt library project which has only 1 class with this 2 functions:

    testclass.h

    #include <QDebug>
    #include <jni.h>
    
    extern "C"
    {
        JNIEXPORT void JNICALL Java_com_example_MyTestAndroidApp_LibraryClass_log();
    
        void logNormal();
    }
    

    testclass.cpp

    JNIEXPORT void JNICALL Java_com_example_MyTestAndroidApp_LibraryClass_log()
    {
        qDebug() << "---> log from Qt library";
    }
    
    void logNormal()
    {
        qDebug() << "---> log from Qt library";
    }
    

    An in the .pro file I am using shared or staticlib to switch from shared(.so) and static(.a) library.

    TEMPLATE = lib
    CONFIG += shared
    #CONFIG += staticlib
    CONFIG += c++11
    

    After successfully building the lib (shared and static) I got this files(for ABI x86 but others ABIS have similar sizes too):

    libTestLibrary.so ---> 3,359 KB

    libTestLibrary.a ---> 7 KB

    For the shared (.so) library I was able to successfully call the function from android app. In Android Studio I am creating new Native C++ project. I have Java JNI LibraryClas which is used to load the library and call the log() function from the library. I placed the library in jniLibs folder.

    But for the static(.a) library there are a lot of problems. I tried to call the logNormal() function from the C++ part of the android app(from native-lib.cpp). I am not using JNI like in the shared library case. Here is what I tried:

    1. Create new Native C++ project
    2. Created libs(for the static library) and include(for testclass.h) folders inside app\src\main\cpp
    3. Inside CMakeList.txt I have added this:
    add_library(TestLibrary STATIC IMPORTED)
    
    set_target_properties(TestLibrary PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libTestLibrary.a)
    
    target_link_libraries( native-lib TestLibrary ${log-lib} )
    
    1. And I called logNormal(); from native-lib.cpp

    First I got Error about missing Qt5Core library. After adding it the same way like my library I got errors for others missing libraries and files.

    I have this questions:

    1. Why static library is so much smaller then the shared library? (7KB vs 3,359KB) It looks like shared lib is including automatically what it needs to run, while static lib is not.
    2. Can I somehow build my static lib with all libraries and files that it need, so I don't get errors for missing libraries?
    3. Can I use JNI to call C++ functions from static lib? Or when calling C++ functions from static lib it must be done from the C++ part of the android project?

    Sorry for the long post and more then 1 question. Any hint or help is welcome. Thank you so much in advance.

    1 Reply Last reply
    0
    • mmjvoxM Offline
      mmjvoxM Offline
      mmjvox
      wrote on last edited by
      #2

      1: I think you don't have any static kit of qt for android and you are using dynamic kit for static build.
      2: I'm trying to build a static kit for android, but i have not succeeded yet.
      3: no, from jni you cant use static libs (.a), but you can build a shared library statically (build static .so file).

      1 Reply Last reply
      2
      • advmA Offline
        advmA Offline
        advm
        wrote on last edited by
        #3

        @mmjvox thank you for the answer and sorry for late respond. You are right, I solved this as you mentioned in the 3.

        In android studio I used JNI to call functions from the shared library, but inside that shared library I called functions from the static library, that was included in the Android Studio Project via CMake.

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved