【问题标题】:Linking static c libraries and getting error recompile with -fPIC使用 -fPIC 链接静态 c 库并重新编译错误
【发布时间】:2019-02-09 10:01:55
【问题描述】:

得到很多“未定义的引用”错误。我的CMakeLists.txt 中缺少什么?我没有正确链接库吗?

native-lib 应该链接到 liblept.a 和 libtesseract.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'

还有更多参考错误

【问题讨论】:

  • 在 unix (etc) 中,您有时必须确保库多次出现在链接器命令行上。例如,如果 libX.a 定义 foo,使用 bar,而 libY.a 使用 foo 并定义 bar,则链接器可能需要查看 -l libX -l libY -l libX -l libY。我不知道android是否是这样的,但可能值得检查未定义的引用是否实际上在您正在链接的库中,如果是,请查看您是否可以说服您的工具多次将列表传递给链接器
  • 它实际上尝试与liblept.a 和libtesseract.a 链接,但发现这些文件不是库:在您的日志中查找错误no archive symbol table (run ranlib) 和invalid character。
  • 你的问题得到答案了吗?
  • 是的,我通过使用 -fPIC 重新编译库解决了我的错误

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


【解决方案1】:

我已经通过简单地重新编译带有 -fPIC 标志的库来解决我的错误。

像这样在makefile中:

CFLAGS += -fPIC
CPPFLAGS += -fPIC

【讨论】:

  • Android 的原生代码应该总是使用 -fPIC 编译。这是平台输入法的基本要求。
【解决方案2】:

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

这也适用于libtesseract 和其他库。

【讨论】:

    猜你喜欢
    • 2013-09-20
    • 2021-05-29
    • 1970-01-01
    • 1970-01-01
    • 2021-09-03
    • 2020-08-22
    • 1970-01-01
    • 2013-08-25
    相关资源
    最近更新 更多