如何在 ndk-build 期间解决 "Relocations in Generic ELF"?

How to resolve "Relocations in Generic ELF" during ndk-build?

我正在尝试使用独立工具链为具有自己的构建过程的项目构建静态库,然后我用 C++ 库包装它并公开给 Android(使用 ndk-build 编译).但是,在 ndk-build 步骤中,我收到以下错误:

➜  jni /Users/chrisfosterelli/Library/Android/sdk/ndk-bundle/ndk-build                    
[arm64-v8a] Compile++      : wrapper <= wrapper.cpp
In file included from /Users/chrisfosterelli/workspace/android/jni/wrapper.cpp:9:
In file included from ../prebuild/include/valhalla/meili/universal_cost.h:7:
In file included from ../prebuild/include/valhalla/sif/dynamiccost.h:4:
In file included from ../prebuild/include/valhalla/baldr/directededge.h:5:
../prebuild/include/valhalla/baldr/graphconstants.h:432:11: warning: 21 enumeration values not handled in switch: 'kRoad', 'kRamp', 'kTurnChannel'... [-Wswitch]
  switch (use) {
          ^
1 warning generated.
[arm64-v8a] SharedLibrary  : libwrapper.so
/Users/chrisfosterelli/Library/Android/sdk/ndk-bundle/toolchains/aarch64-linux-android-4.9/prebuilt/darwin-x86_64/lib/gcc/aarch64-linux-android/4.9.x/../../../../aarch64-linux-android/bin/ld: /Users/chrisfosterelli/workspace/android/jni/../prebuild/libvalhalla_meili.a(libvalhalla_meili_la-map_matcher_factory.o): Relocations in generic ELF (EM: 62)
[...above message repeated many times...]
/Users/chrisfosterelli/workspace/android/jni/../prebuild/libvalhalla_meili.a: error adding symbols: File in wrong format
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [/Users/chrisfosterelli/workspace/android/obj/local/arm64-v8a/libwrapper.so] Error 1
➜  jni ls /Users/chrisfosterelli/Library/Android/sdk/ndk-bundle/

据我所知,此错误表明我正在尝试混合和匹配为不同体系结构编译的二进制文件。但是,据我所知,图书馆的架构是正确的:

root@eacbdb1c0e46:/meili/meili2/newtest# ar x libvalhalla_meili.a 
root@eacbdb1c0e46:/meili/meili2/newtest# file libvalhalla_meili_la-map_matcher_factory.o 
libvalhalla_meili_la-map_matcher_factory.o: ELF 64-bit LSB relocatable, ARM aarch64, version 1 (SYSV), not stripped
root@eacbdb1c0e46:/meili/meili2/newtest#

FWIW,这些是我目前的 Application.mk

APP_STL := c++_static
APP_CPPFLAGS := -frtti -std=gnu++11 -D_GLIBCXX_USE_C99
APP_CPPFLAGS += -fexceptions
NDK_TOOLCHAIN_VERSION := clang
APP_LDFLAGS := -latomic
APP_PLATFORM := android-21
APP_ABI := arm64-v8a

Android.mk,

LOCAL_PATH := $(call my-dir)

# static library info
include $(CLEAR_VARS)
LOCAL_MODULE := libvalhalla_meili
LOCAL_SRC_FILES := ../prebuild/libvalhalla_meili.a
LOCAL_EXPORT_C_INCLUDES := ../prebuild/include
include $(PREBUILT_STATIC_LIBRARY)

# wrapper info
include $(CLEAR_VARS)
LOCAL_C_INCLUDES += ../prebuild/include
LOCAL_MODULE    := wrapper
LOCAL_SRC_FILES := wrapper.cpp
LOCAL_STATIC_LIBRARIES := libvalhalla_meili
include $(BUILD_SHARED_LIBRARY)

有什么解决办法吗?我已经尝试了很多方法,但所有这些都会导致更多(更模糊)的错误,所以我希望这里有人能给我指明正确的方向!

我发现了这个问题,但我希望我有一个更有见地的答案。显然,构建过程创建了两个库文件。我检查的那个当然是 ARM64。然而,我实际复制到 Android 设备的是 x86。

所以,错误信息是正确的,库文件也是正确的,但是涉及的文件不止一个。如果您 运行 遇到同样的问题并且非常确定您的库是 ARM64,请仔细检查它实际上是被编译到 ndk 构建中的同一个文件!