Search code examples
androidccmakeandroid-ndkndk-build

Linking static c libraries and getting error recompile with -fPIC


Getting a lot of "undefined reference to" errors. What I am missing in my CMakeLists.txt? Am I not linking the library properly?

native-lib should be linked to liblept.a and libtesseract.a libraries which I am thinking is not being linked to these libraries.

Here is my CMakeLists.txt:

cmake_minimum_required(VERSION 3.4.1)


set(distribution_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../../../distribution)

add_library(lib_musireader STATIC IMPORTED)
set_target_properties(lib_musireader PROPERTIES IMPORTED_LOCATION
        ${distribution_DIR}/musireader/lib/${ANDROID_ABI}/libMusiReader.a)

add_library(lib_lept STATIC IMPORTED)
set_target_properties(lib_lept PROPERTIES IMPORTED_LOCATION
        ${distribution_DIR}/leptonica/lib/${ANDROID_ABI}/liblept.a)

add_library(lib_tesseract STATIC IMPORTED)
set_target_properties(lib_tesseract PROPERTIES IMPORTED_LOCATION
        ${distribution_DIR}/tesseract/lib/${ANDROID_ABI}/libtesseract.a)
add_library( # Sets the name of the library.
        native-lib
        SHARED
        native_lib.c)

find_library( 
        log-lib
        log)

target_include_directories(native-lib PRIVATE
        ${distribution_DIR}/musireader/include
        ${distribution_DIR}/leptonica/include
        ${distribution_DIR}/tesseract/include)

target_link_libraries( # Specifies the target library.
        native-lib
        lib_musireader
        lib_lept
        lib_tesseract


        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

Here is build errors which I am getting:

Build command failed.

    Error while executing process /home/mushahid/Android/Sdk/cmake/3.6.4111459/bin/cmake with arguments {--build /home/mushahid/AndroidStudioProjects/MyApplication/app/.externalNativeBuild/cmake/debug/x86_64 --target native-lib}
    [1/2] Building C object CMakeFiles/native-lib.dir/native_lib.c.o
    [2/2] Linking C shared library /home/mushahid/AndroidStudioProjects/MyApplication/app/build/intermediates/cmake/debug/obj/x86_64/libnative-lib.so
    FAILED: : && /home/mushahid/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang  --target=x86_64-none-linux-android23 --gcc-toolchain=/home/mushahid/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64 -fPIC --sysroot /home/mushahid/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fno-addrsig -Wa,--noexecstack -Wformat -Werror=format-security  -O0 -fno-limit-debug-info  -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libnative-lib.so -o /home/mushahid/AndroidStudioProjects/MyApplication/app/build/intermediates/cmake/debug/obj/x86_64/libnative-lib.so CMakeFiles/native-lib.dir/native_lib.c.o  /home/mushahid/AndroidStudioProjects/MyApplication/app/src/main/cpp/../../../../distribution/musireader/lib/x86_64/libMusiReader.a /home/mushahid/AndroidStudioProjects/MyApplication/app/src/main/cpp/../../../../distribution/leptonica/lib/x86_64/liblept.a /home/mushahid/AndroidStudioProjects/MyApplication/app/src/main/cpp/../../../../distribution/tesseract/lib/x86_64/libtesseract.a /home/mushahid/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/x86_64-linux-android/23/liblog.so -latomic -lm && :
    /home/mushahid/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: /home/mushahid/AndroidStudioProjects/MyApplication/app/src/main/cpp/../../../../distribution/leptonica/lib/x86_64/liblept.a: no archive symbol table (run ranlib)
    /home/mushahid/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: /home/mushahid/AndroidStudioProjects/MyApplication/app/src/main/cpp/../../../../distribution/tesseract/lib/x86_64/libtesseract.a:1:1: invalid character
    /home/mushahid/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: /home/mushahid/AndroidStudioProjects/MyApplication/app/src/main/cpp/../../../../distribution/musireader/lib/x86_64/libMusiReader.a(correlation.c.o): requires dynamic R_X86_64_PC32 reloc against 'LeptMsgSeverity' which may overflow at runtime; recompile with -fPIC
    /home/mushahid/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: /home/mushahid/AndroidStudioProjects/MyApplication/app/src/main/cpp/../../../../distribution/musireader/lib/x86_64/libMusiReader.a(dici.c.o): requires dynamic R_X86_64_PC32 reloc against 'calloc' which may overflow at runtime; recompile with -fPIC
    /home/mushahid/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: /home/mushahid/AndroidStudioProjects/MyApplication/app/src/main/cpp/../../../../distribution/musireader/lib/x86_64/libMusiReader.a(export.c.o): requires dynamic R_X86_64_PC32 reloc against 'LeptMsgSeverity' which may overflow at runtime; recompile with -fPIC
    /home/mushahid/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: /home/mushahid/AndroidStudioProjects/MyApplication/app/src/main/cpp/../../../../distribution/musireader/lib/x86_64/libMusiReader.a(interpreter.c.o): requires dynamic R_X86_64_PC32 reloc against 'LeptMsgSeverity' which may overflow at runtime; recompile with -fPIC
    /home/mushahid/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: /home/mushahid/AndroidStudioProjects/MyApplication/app/src/main/cpp/../../../../distribution/musireader/lib/x86_64/libMusiReader.a(log.c.o): requires dynamic R_X86_64_PC32 reloc against 'boxCreate' which may overflow at runtime; recompile with -fPIC
    /home/mushahid/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: /home/mushahid/AndroidStudioProjects/MyApplication/app/src/main/cpp/../../../../distribution/musireader/lib/x86_64/libMusiReader.a(preprocessor.c.o): requires dynamic R_X86_64_PC32 reloc against 'pixCreate' which may overflow at runtime; recompile with -fPIC
    /home/mushahid/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: /home/mushahid/AndroidStudioProjects/MyApplication/app/src/main/cpp/../../../../distribution/musireader/lib/x86_64/libMusiReader.a(recognition.c.o): requires dynamic R_X86_64_PC32 reloc against 'boxCreate' which may overflow at runtime; recompile with -fPIC
    /home/mushahid/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: /home/mushahid/AndroidStudioProjects/MyApplication/app/src/main/cpp/../../../../distribution/musireader/lib/x86_64/libMusiReader.a(rle.c.o): requires dynamic R_X86_64_PC32 reloc against 'LeptMsgSeverity' which may overflow at runtime; recompile with -fPIC
    /home/mushahid/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: /home/mushahid/AndroidStudioProjects/MyApplication/app/src/main/cpp/../../../../distribution/musireader/lib/x86_64/libMusiReader.a(segmentator.c.o): requires dynamic R_X86_64_PC32 reloc against 'LeptMsgSeverity' which may overflow at runtime; recompile with -fPIC
    /home/mushahid/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: /home/mushahid/AndroidStudioProjects/MyApplication/app/src/main/cpp/../../../../distribution/musireader/lib/x86_64/libMusiReader.a(segments.c.o): requires dynamic R_X86_64_PC32 reloc against 'LeptMsgSeverity' which may overflow at runtime; recompile with -fPIC
    /home/mushahid/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: /home/mushahid/AndroidStudioProjects/MyApplication/app/src/main/cpp/../../../../distribution/musireader/lib/x86_64/libMusiReader.a(serialization.c.o): requires dynamic R_X86_64_PC32 reloc against 'fseek' which may overflow at runtime; recompile with -fPIC
    /home/mushahid/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: /home/mushahid/AndroidStudioProjects/MyApplication/app/src/main/cpp/../../../../distribution/musireader/lib/x86_64/libMusiReader.a(staff.c.o): requires dynamic R_X86_64_PC32 reloc against 'calloc' which may overflow at runtime; recompile with -fPIC
    /home/mushahid/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: /home/mushahid/AndroidStudioProjects/MyApplication/app/src/main/cpp/../../../../distribution/musireader/lib/x86_64/libMusiReader.a(staff_detector.c.o): requires dynamic R_X86_64_PC32 reloc against 'staffaCreate' which may overflow at runtime; recompile with -fPIC
    /home/mushahid/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: /home/mushahid/AndroidStudioProjects/MyApplication/app/src/main/cpp/../../../../distribution/musireader/lib/x86_64/libMusiReader.a(sym_recognizer.c.o): requires dynamic R_X86_64_PC32 reloc against 'boxCreate' which may overflow at runtime; recompile with -fPIC
    /home/mushahid/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: error: /home/mushahid/AndroidStudioProjects/MyApplication/app/src/main/cpp/../../../../distribution/musireader/lib/x86_64/libMusiReader.a(utility.c.o): requires dynamic R_X86_64_PC32 reloc against 'LeptMsgSeverity' which may overflow at runtime; recompile with -fPIC
    /home/mushahid/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/x86_64-linux-android/4.9.x/../../../../x86_64-linux-android/bin/ld: warning: shared library text segment is not shareable


    /home/mushahid/Project/Project/sources/correlation.c:13: error: undefined reference to 'LeptMsgSeverity'
    /home/mushahid/Project/Project/sources/correlation.c:13: error: undefined reference to 'returnErrorInt'
    /home/mushahid/Project/Project/sources/correlation.c:15: error: undefined reference to 'LeptMsgSeverity'
    /home/mushahid/Project/Project/sources/correlation.c:15: error: undefined reference to 'returnErrorInt'

and lot more reference errors


Solution

  • I have resolved my errors by simply recompiling the libraries with -fPIC flag.

    like this in makefile:

    CFLAGS += -fPIC
    CPPFLAGS += -fPIC