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

Linking static c libraries and getting error recompile with -fPIC

出现很多 "undefined reference to" 错误。我的 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'

还有更多参考错误

您正在链接 lib_lept,但您的库名称是 liblept,因此您需要将所有出现的 lib_lept 更改为 liblept,或者更改您的库名称 liblept.alib_lept.a 随你喜欢。

这也适用于 libtesseract 和其他图书馆。

我通过使用 -fPIC 标志重新编译库解决了我的错误。

在 makefile 中像这样:

CFLAGS += -fPIC
CPPFLAGS += -fPIC