如何从 NDK-build 静态库中解析 NDK-build 静态库中的 "undefined reference to <myfunction>"?

How to resolve "undefined reference to <myfunction>" in NDK-built static library from NDK-built static library?

我得到了一个 "undefined reference to 'myfunction' trying to call a function in a static (.a) library from a shared (.so) library. (This is an NDK question on OS X). The function is part of the fft library called "ffts-android" (ffts-android)。我能够很好地使用该项目生成库。我所做的唯一更改是从 BUILD_SHARED_LIBRARY 到 BUILD_STATIC_LIBRARY,这样我就可以将它合并到我们组织的 Android 应用程序中使用的共享库中。

我很确定我需要的函数 ffts_init_1d(size_t, int) 存在于静态库中,因为我使用了 nm 实用程序(底部的输出)来列出符号 table.

中可用的函数

然后我想我会尝试修改 NDK 的一个简单示例程序,以防我的 Android.mk 出现问题。我采用了 "module-exports" 示例并进行了必要的最简单的更改并得到了相同的错误。在其原始状态下,它构建了一个静态库,然后将其链接到一个共享库中,就像我正在尝试做的那样。我只是在 Android.mk 中添加了一行,即下面的中间一行,以便它链接到我从 ffts-android 项目构建的同一个库:

LOCAL_STATIC_LIBRARIES := foo
LOCAL_STATIC_LIBRARIES += libffts-neon
include $(BUILD_SHARED_LIBRARY)

然后修改 bar.c 以便它调用相同的方法,ffts_init_1d(size_t, int) 我正在尝试调用:

#include "bar.h"
#include "ffts.h"
#include "foo.h"

int  bar(int  x)
{
    /*return*/foo(x)-1;
    ffts_init_1d(1, 1);
    return 1;
}

最后,我将 ffts.h 和 libffts-neon.a 文件移动到模块导出可以找到它们的位置,然后构建示例。它会产生与我得到的完全相同的错误。

samples/module-exports/jni/bar/bar.c:8: undefined reference to `ffts_init_1d'

module-exports 项目和 ffts-android 项目都是纯 c 代码,而我们的代码库是 c++,所以我试图从 c++ 调用从 c 代码构建的静态库。头文件,ffts.h,不过已经有了适应这个的所需代码,标准:

#ifdef __cplusplus
extern "C"
{

以下是 nm 实用程序的输出。在我看来列出了我要调用的函数,为什么它不可用?

./nm -g libffts-neon.a

ffts.o:
         U _GLOBAL_OFFSET_TABLE_
         U __aeabi_uidiv
         U __aeabi_uidivmod
         U __aeabi_unwind_cpp_pr0
         U __aeabi_unwind_cpp_pr1
         U __android_log_print
         U __errno
         U cos
         U exit
00000000 T ffts_execute
00000000 T ffts_free
00000000 T ffts_free_1d
         U ffts_generate_func_code
00000000 T ffts_init_1d
         U ffts_init_is
         U ffts_init_offsets
         U firstpass_16_b
         U firstpass_16_f
         U firstpass_2
         U firstpass_4_b
         U firstpass_4_f
         U firstpass_8_b
         U firstpass_8_f
         U free
         U malloc
         U mprotect
         U munmap
         U perror
         U sin
         U valloc

ffts_real.o:
         U __aeabi_unwind_cpp_pr0
         U __aeabi_unwind_cpp_pr1
         U cos
00000000 T ffts_execute_1d_real
00000000 T ffts_execute_1d_real_inv
         U ffts_free
00000000 T ffts_free_1d_real
         U ffts_init_1d
00000000 T ffts_init_1d_real
         U free
         U malloc
         U sin
         U valloc
. . .

这是我使用 V=1 调用 ndk-build 时得到的结果(详细输出):

MacBook-Pro:module-exports myuser$ ndk-build V=1
rm -f ./libs/arm64-v8a/lib*.so ./libs/armeabi/lib*.so ./libs/armeabi-v7a/lib*.so ./libs/armeabi-v7a-hard/lib*.so ./libs/mips/lib*.so ./libs/mips64/lib*.so ./libs/x86/lib*.so ./libs/x86_64/lib*.so
rm -f ./libs/arm64-v8a/gdbserver ./libs/armeabi/gdbserver ./libs/armeabi-v7a/gdbserver ./libs/armeabi-v7a-hard/gdbserver ./libs/mips/gdbserver ./libs/mips64/gdbserver ./libs/x86/gdbserver ./libs/x86_64/gdbserver
rm -f ./libs/arm64-v8a/gdb.setup ./libs/armeabi/gdb.setup ./libs/armeabi-v7a/gdb.setup ./libs/armeabi-v7a-hard/gdb.setup ./libs/mips/gdb.setup ./libs/mips64/gdb.setup ./libs/x86/gdb.setup ./libs/x86_64/gdb.setup
[armeabi-v7a] SharedLibrary  : libbar.so
/Users/myuser/Documents/NDKDev/android-ndk-r10e/toolchains/arm-linux-androideabi-4.8/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-g++ -Wl,-soname,libbar.so -shared --sysroot=/Users/myuser/Documents/NDKDev/android-ndk-r10e/platforms/android-3/arch-arm ./obj/local/armeabi-v7a/objs/bar/bar/bar.o ./obj/local/armeabi-v7a/libfoo.a -lgcc -no-canonical-prefixes -march=armv7-a -Wl,--fix-cortex-a8  -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -mthumb  -L/Users/myuser/Documents/NDKDev/android-ndk-r10e/platforms/android-3/arch-arm/usr/lib -llog -lc -lm -o ./obj/local/armeabi-v7a/libbar.so
jni/bar/bar.c:8: error: undefined reference to 'ffts_init_1d'
collect2: error: ld returned 1 exit status
make: *** [obj/local/armeabi-v7a/libbar.so] Error 1

这是 Android.mk 文件。稍作修改,如下所述:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := foo
LOCAL_SRC_FILES := foo/foo.c
LOCAL_CFLAGS := -DFOO=2
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/foo
LOCAL_EXPORT_CFLAGS := -DFOO=1
LOCAL_EXPORT_LDLIBS := -llog
include $(BUILD_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := bar
LOCAL_SRC_FILES := bar/bar.c
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/bar
LOCAL_STATIC_LIBRARIES := libffts-neon
LOCAL_STATIC_LIBRARIES += foo
include $(BUILD_SHARED_LIBRARY)

您必须定义预建静态库 libffts-neon.a 供您的模块使用。这个想法是每个库(静态的或共享的,预构建的或编译的)都应该有其单独的部分并分配其单独的 LOCAL_MODULE 名称。所以,你错过了以下附魔:

include $(CLEAR_VARS)
LOCAL_MODULE := libffts-neon
LOCAL_SRC_FILES := obj/local/armeabi-v7a/libffts-neon.a
include $(PREBUILT_STATIC_LIBRARY)

这可以放在 Android.mk 中的任何位置,或者使用 include 指令并将其保存在一个单独的文件中。

注意 LOCAL_STATIC_LIBRARIES 指的是 模块名称 ,而不是实际文件的名称。所以,以下也可以:

include $(CLEAR_VARS)
LOCAL_MODULE := qqq
LOCAL_SRC_FILES := obj/local/armeabi-v7a/libffts-neon.a
include $(PREBUILT_STATIC_LIBRARY)
…
LOCAL_STATIC_LIBRARIES += qqq
include $(BUILD_SHARED_LIBRARY)

另外请注意,您可以将预构建库放在任何地方,ndk-build 会根据需要将其复制到适当的目录。

官方 documentation 非常详细,解决了多 ABI 支持和预构建库的其他较暗角落的问题。