【问题标题】:Undefined reference to '_Unwind_GetIP'对“_Unwind_GetIP”的未定义引用
【发布时间】:2015-11-03 03:43:28
【问题描述】:

我在将 rustc 编译为 staticlib 三倍 arm-linux-androideabi 以在 Android Studio 中很好地链接时遇到了一些麻烦。

采取的步骤...

  1. 通过multirust 安装 Rust
  2. 为 Android API v14 构建一个 rustc
  3. 使用--target=arm-linux-androideabi 构建库
  4. 在 Android Studio 中添加到 jniLibs/
  5. 为与 JNI 之间的钩子创建一个小的 C++ shim
  6. 构建和链接(几乎)

在构建我的 crate 时,我得到以下输出:

note: link against the following native artifacts when linking 
      against this static library
note: the order and any duplication can be significant on some platforms, 
      and so may need to be preserved
note: library: c
note: library: m
note: library: dl
note: library: log
note: library: gcc
note: library: c
note: library: m

所以很自然,我的 Android.mk 在使用静态库时包含这些库。

Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := hydrogen
LOCAL_SRC_FILES := ../jniLibs/$(TARGET_ARCH_ABI)/libhydrogen.a
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := hydrogen-android
LOCAL_SRC_FILES := shim.cpp
LOCAL_STATIC_LIBRARIES := hydrogen
LOCAL_LDLIBS := -lc -lm -ldl -llog -lgcc -lc -lm
include $(BUILD_SHARED_LIBRARY)

在 Android Studio 中构建会得到以下输出:

:app:buildCppShim
Android NDK: WARNING:/Android.mk:hydrogen: non-system libraries in linker flags: -lgcc -lgccunwind    
Android NDK:     This is likely to result in incorrect builds. Try using LOCAL_STATIC_LIBRARIES    
Android NDK:     or LOCAL_SHARED_LIBRARIES instead to list the library dependencies of the    
Android NDK:     current module    

[armeabi] Compile++ thumb: hydrogen-android <= shim.cpp
[armeabi] SharedLibrary  : libhydrogen-android.so
[snipped]function sync::rwlock::StaticRwLock::read::ha5ec9717ccd1ed83Lxp: error: undefined reference to 'pthread_rwlock_rdlock'
[snipped]function sys_common::rwlock::RWLock::read::h7f3d472c79e2e1e2Z1q: error: undefined reference to 'pthread_rwlock_rdlock'
[snipped]function sys_common::rwlock::RWLock::read_unlock::hd7d67e9c5c47b9f5B2q: error: undefined reference to 'pthread_rwlock_unlock'
[snipped]function sync::rwlock::StaticRwLock::try_read::hddd396186cced62f8xp: error: undefined reference to 'pthread_rwlock_tryrdlock'
[snipped]function sys_common::rwlock::RWLock::try_read::ha5aede723e91a3c881q: error: undefined reference to 'pthread_rwlock_tryrdlock'
[snipped]function sync::rwlock::StaticRwLock::write::h787666bb30e75d28Ryp: error: undefined reference to 'pthread_rwlock_wrlock'
[snipped]function sys_common::rwlock::RWLock::write::h0273da9a7ade68c0i2q: error: undefined reference to 'pthread_rwlock_wrlock'
[snipped]function sync..rwlock..RwLockWriteGuard$LT$$LP$$RP$$GT$::drop.34348::h4c8fbe45843b9a01: error: undefined reference to 'pthread_rwlock_unlock'
[snipped]function sync::rwlock::StaticRwLock::try_write::h5d30a7fdd53c86b4ezp: error: undefined reference to 'pthread_rwlock_trywrlock'
[snipped]function sys_common::rwlock::RWLock::try_write::h0ec4bcc0cb460718r2q: error: undefined reference to 'pthread_rwlock_trywrlock'
[snipped]function sync::rwlock::StaticRwLock::destroy::ha1e9f51e62905aedXzp: error: undefined reference to 'pthread_rwlock_destroy'
[snipped]function sys_common::rwlock::RWLock::destroy::h602ce773ff2356e6T2q: error: undefined reference to 'pthread_rwlock_destroy'
[snipped]function sys::rwlock::RWLock::read::h241f5fdff06a76ab00u: error: undefined reference to 'pthread_rwlock_rdlock'
[snipped]function sys::rwlock::RWLock::write::h48034b52e6491ea4h3u: error: undefined reference to 'pthread_rwlock_wrlock'

~/bin/rust/src/compiler-rt/lib/builtins/gcc_personality_v0.c
Error:(206) undefined reference to '_Unwind_GetIP'
Error:(273) undefined reference to '_Unwind_SetGR'
Error:(274) undefined reference to '_Unwind_SetGR'
Error:(275) undefined reference to '_Unwind_SetIP'

线程错误 我不确定为什么会收到关于 pthread 的错误,因为我的 shim 中有 #include &lt;pthread.h&gt;,而这些函数在 Android's pthread 中定义。

展开错误 为此,我尝试在 shim 中包含 unwind.h,在 Android.mk 中包含 libgccunwind.a,但仍然抛出相同的错误。我找到了this thread,其中一个黑客解决方案就是在 shim 中声明原型,但是当我这样做时,我收到以下关于 unwind.h 的错误

Error:(231, 3) error: previous declaration 'void _Unwind_SetGR(_Unwind_Context*, int, _Unwind_Word)' here

所以,当我声明它们时,它能够找到它们的声明,因为它们冲突,但如果我不知道它们在哪里?我假设我的链接顺序一定有问题,但不确定如何解决它,因为在拉入 rust 创建的库后,需要的库被链接。

任何帮助将不胜感激!

编辑 1 调整了在共享库创建期间链接的 makefile。

【问题讨论】:

    标签: makefile android-ndk linker rust cross-compiling


    【解决方案1】:

    LOCAL_LDLIBS 对于静态库没有意义(静态库不会被链接)。每当您在真正的二进制文件(如共享库或可执行文件)中使用该库时,都需要添加这些库。

    【讨论】:

    • 没有帮助,请参阅上面的编辑进行调整。发生同样的错误。
    【解决方案2】:

    解决方案恰好是构建 unwind 并包含一些可移植性标头。见模块示例here

    【讨论】:

    • nice:) 你能用命令和/或链接器/编译器标志详细说明答案吗?
    猜你喜欢
    • 2011-03-27
    • 2022-01-22
    • 2016-04-04
    • 2011-08-11
    • 2019-07-22
    • 2015-07-20
    • 2019-06-22
    • 2012-04-02
    • 2021-02-04
    相关资源
    最近更新 更多