android - OS X 和 Android 共享库中覆盖弱函数的不同行为

标签 android android-ndk cross-platform weak-linking

我遇到了 OS X 和 Android 之间的不同行为:

  • 我的共享库中有一个弱函数foo
  • 我想用我的可执行文件中定义的强大函数覆盖它。
  • 我希望重写也会影响库内的调用

结果:我在 OS X 上得到了预期的结果,但在 Android 上失败了。


这是我的测试项目:

文件:shared.h

void library_call_foo();
void __attribute__((weak)) foo();

文件:shared.c

#include "shared.h"
#include <stdio.h>

void library_call_foo()
{
    printf("shared library call foo -> ");
    foo();
}

void foo()
{
    printf("weak foo in library\n");
}

文件:main.c

#include <stdio.h>
#include <shared.h>

void foo()
{
    printf("strong foo in main\n");
}

int main()
{
    library_call_foo();
    printf("main call foo -> ");
    foo();
    return 0;
}

我在 OS X 中使用命令编译并运行它:

clang -shared -fPIC -o libshared.so shared.c
clang -I. -L. -lshared -o test main.c
./test

如我所料返回结果:

shared library call foo -> strong foo in main
main call foo -> strong foo in main

但是当我使用 NDK 工具链为 Android 编译它时使用相同的命令:

arm-linux-androideabi-clang -shared -fPIC -o libshared.so shared.c
arm-linux-androideabi-clang -I. -L. -lshared -o test main.c

然后在设备上运行它,我得到了不同的结果:

shared library call foo -> weak foo in library
main call foo -> strong foo in main

为什么行为不同,我该如何解决?

最佳答案

Android 动态链接器实际上支持弱符号。问题是这个特殊情况是库是用 -Bsymbolic 编译的(检查这个运行“readelf -d libshared.so”)。

解决此问题的方法是在链接库时使用“-Wl,-shared”而不是“-shared”。

请参阅https://code.google.com/p/android/issues/detail?id=68956了解详细信息和解决方法。

关于android - OS X 和 Android 共享库中覆盖弱函数的不同行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20196368/

相关文章:

android - 为什么 Tesseract for Android 在使用 "equ.traineddata"检测数学符号或方程时返回乱码?

android - Android上 native 堆的最大大小?

c++ - 如何定义自定义比较函数以根据一维数组排序对矩阵进行排序

c++ - 什么是跨平台字符串类的 "Best Practices"以实现良好的模型可移植性?

java - 关于 Android Gallery 控件的一般问题

android - ListView 图像仅在我滚动(并消失)时显示。解释它是如何工作的

Android:保存到 SD 卡总是在重新安装时删除

android - 系统/fcntl.h : no such file while cross compiling pcsclite for Android?

python - 给 python "platform"库虚假平台信息?

安卓工作室 : Error converting bytecode to dex:Multiple dex files define Ljavax/mail/internet/ParameterList$ToStringBuffer;