0

I'm trying to create a static library that takes an image, do some image processing using OpenCV and finally return a string. I decide to use my own static library to use in android platform. First of all, I build OpenCV for the android platform (or just download OpenCV SDK for android). So I have both shared and static library of OpenCV. after that, I tried to build my own library that uses these OpenCV modules: core, imgproc, dnn

and finally, I had .a file with 1.2 MB of size. but when I added my library to android I got compiled error :

undefined reference to 'cv::Sobel(cv::_InputArray const&, cv::_OutputArray const&, int, int, int, int, double, double, int)'

It seems that my library did not include OpenCV modules. I was searching a lot about this issue but I got nothing and almost try everything. Here is cmakeList.txt for my library :

    if (" ${CMAKE_SOURCE_DIR}" STREQUAL " ${CMAKE_BINARY_DIR}")
        message(FATAL_ERROR "FATAL: In-source builds are not allowed.
           You should create a separate directory for build files.")
    endif ()

    cmake_minimum_required(VERSION 3.14)
    project(scanner)

    set(CMAKE_CXX_STANDARD 14)
    set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR/lib})

    set(SCANNER_LIBRARY_SOURCES business/ImageProcessor.cpp
            util/ContoursUtil.cpp
            Predictor.cpp)

add_library(scanner STATIC ${SCANNER_LIBRARY_SOURCES})

    include_directories(/Users/shayantabatabaei/Downloads/sdk/sdk/native/jni/include)
    target_link_libraries(scanner INTERFACE
            /Users/shayantabatabaei/Downloads/sdk/sdk/native/staticlibs/x86/libopencv_core.a
            /Users/shayantabatabaei/Downloads/sdk/sdk/native/staticlibs/x86/libopencv_dnn.a
            /Users/shayantabatabaei/Downloads/sdk/sdk/native/staticlibs/x86/libopencv_imgproc.a)

    install(FILES Predictor.h
            DESTINATION ${CMAKE_BINARY_DIR}/include)

and I build my library whit this command :

cmake .. -DCMAKE_TOOLCHAIN_FILE=$ANDROID_NDK/build/cmake/android.toolchain.cmake -DANDROID_NDK=$ANDROID_NDK -DANDROID_NATIVE_API_LEVEL=android-28 -DANDROID_STL=c++_static -DBUILD_SHARED_LIBS=OFF -DCMAKE_INSTALL_PREFIX:PATH=$(pwd) -DANDROID_ABI=${TARGET};

thanks.

GoldenAge
  • 2,918
  • 5
  • 25
  • 63
  • "It seems that my library did not include OpenCV modules." - Yes, a **static** library has never included other libraries. That is, when you link something with a static library, you need to link also with other libraries, which the static library depends from. In CMake `INTERFACE` linkage is useful when use the library from the same project, or when your project installs `ConfigXXX.cmake` file, so it can be used by other projects by `find_package()`. – Tsyvarev Jul 28 '19 at 17:41
  • @Tsyvarev : Thanks for your response, Is there any way to link shared libraries together and finally get a single file of shared libraries ? and in my case which type of libraries should I use ? – Shayan Tabatabaee Jul 30 '19 at 07:34
  • 1
    " Is there any way to link shared libraries together and finally get a single file of shared libraries?" - No, there is no way to merge **shared** libraries: https://stackoverflow.com/questions/14987258/can-unix-shared-libraries-be-merged-into-a-single-library. It is possible to merge **static** libraries: https://stackoverflow.com/questions/37924383/combining-several-static-libraries-into-one-using-cmake. Another approach: when distribute your library, you may also distribute libraries it depends from (e.g. OpenCV). This approach can be used whether libraries are static or dynamic. – Tsyvarev Jul 30 '19 at 07:51
  • @Tsyvarev : Thank you , I'll take a look at links – Shayan Tabatabaee Jul 30 '19 at 08:29

0 Answers0