链接静态C库并使用-fPIC重新编译错误

时间:2019-02-09 10:01:56

标签: android c cmake android-ndk ndk-build

获得很多“未定义的引用”错误。 CMakeLists.txt中我缺少什么?我是否无法正确链接库?

native-lib应该链接到我认为没有链接到这些库的liblept.alibtesseract.a库。

这是我的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})

这是我得到的构建错误:

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'

更多参考错误

2 个答案:

答案 0 :(得分:1)

我已经解决了我的错误,只需使用 -fPIC 标志重新编译库即可。

在makefile中是这样的:

CFLAGS += -fPIC
CPPFLAGS += -fPIC

答案 1 :(得分:0)

您要链接到lib_lept,但是您的库名是liblept,因此您需要将所有出现的lib_lept更改为liblept,或更改您的库名liblept.alib_lept.a中的任意一个。

这也适用于libtesseract和其他库。