需要 'cpufeatures' 的外部 NDK 库的链接器错误

Linker errors with external NDK library that needs 'cpufeatures'

我正在尝试构建 link freeimage 到 android 项目。我很接近,但我被那个库中的一些 linker 错误绊倒了。

我正在使用这个存储库:https://github.com/jamcar23/FreeImage-Android/blob/master/jni/freeimage/Android.mk

Freeimage 使用内部 NDK 库 'cpufeatures' 来使用至强芯片组功能。在项目的 'android.mk' 中,有对 cpufeatures 库的引用:

LOCAL_STATIC_LIBRARIES := cpufeatures

和我的图书馆,它静态地 links 到这个,还在该项目的 android.mk:

的 LOCAL_STATIC_LIBRARIES 声明中包含 cpufeatures
LOCAL_STATIC_LIBRARIES := tinyxml freetype2 bullet freeimage cpufeatures

也在我的 android.mk 中,我 link freeimage 是这样的:

#####FREEIMAGE_LIBRARY_DECLARATION##########
include $(CLEAR_VARS)
LOCAL_PATH = $(TPLIBROOT)/FreeImage-Android
LOCAL_MODULE := freeimage
LOCAL_EXPORT_C_INCLUDES := include
LOCAL_SRC_FILES := obj/local/$(TARGET_ARCH_ABI)/libFreeImage.a
include $(PREBUILT_STATIC_LIBRARY)
###############################################

注意到我之前关于 NDK 的一个问题,它应该处理特定的架构(我已经使用所有可用的架构构建了 freeimage)

freeimage .a 和 .so 库似乎可以正常构建,但是在构建 .so 时 linking 到我的库时,出现此错误:

[armeabi-v7a] SharedLibrary  : libAnthracite.so
jni/freeimage/Source/LibWebP/./src/dsp/dsp.cpu.c:108: error: undefined reference to 'android_getCpuFamily'
jni/freeimage/Source/LibWebP/./src/dsp/dsp.cpu.c:109: error: undefined reference to 'android_getCpuFeatures'
jni/freeimage/Source/LibWebP/./src/dsp/dsp.dec.c:745: error: undefined reference to 'VP8DspInitNEON'

这很奇怪,因为两个库都有 link cpufeatures,所以它真的应该在那里。

我宣布

APP_PLATFORM := android-14
APP_STL := gnustl_static

在两个项目的 application.mk 文件中。 此外,我尝试将 'LOCAL_STATIC_LIBRARIES' 放在文件中的不同位置,并以不同的顺序 linking 库,尽管这只是猜测。 有谁知道可能导致这些 linker 错误的原因吗?

请按照 official guide 添加 cpu-功能。 TL;NR:将 $(call import-module,android/cpufeatures) 添加到您的 Android.mk.

通过确保我使用的所有四个第三方库的所有 'application.mk' 文件共享一个如下所示的公共基础文件,我终于让它工作了:

APP_PLATFORM := android-15
APP_STL      := c++_static
APP_ABI      := all    
APP_OPTIM    := release
APP_SHORT_COMMANDS := true

这使得它们更容易保持一致,确保它们都是针对相同的库构建的。 此外,我将 STL 实现从 'gnustl_static' 更改为 'c++_static'