gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:03:07 25 4
gpt4 key购买 nike

收到很多“ undefined reference ”错误。我的 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'

以及更多引用错误

最佳答案

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

像这样在makefile中:

CFLAGS += -fPIC
CPPFLAGS += -fPIC

关于android - 使用 -fPIC 链接静态 C 库并重新编译时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54605151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com