Android 是否支持弱符号? [英] Does Android support weak symbols?

查看:17
本文介绍了Android 是否支持弱符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究以下问题:

I've been looking at questions like:

在我看来,这可以通过 weak symbols 解决.也就是说,一个原生组件可以提供像 rand 这样的符号,但是用 __attribute__((weak)) 来装饰它们.如果符号在另一个库中找到,比如标准运行时,那么弱链接符号将 not 被使用.另一方面,如果缺少符号,则将使用本机组件的版本.

It seems to me this could be solved with weak symbols. That is, a native component could provide symbols like rand but adorn them with __attribute__((weak)). If the symbol is found in another library, like the standard runtime, then the weakly linked symbol would not be used. On the other hand, if the symbol was missing, then the native component's version would be used.

我无法在其上查找适用于 Android 的信息(搜索时不相关的噪音过多).

I'm having trouble locating information on it for Android (too much unrelated noise while searching).

我打开了我的 Crypto++/JNI 示例项目之一,并将以下内容添加到 CPP 文件中.AutoSededRandomPool 只是一个 Crypto++ 随机数生成器对象(下面没有什么特别或棘手的).

I opened one of my Crypto++/JNI sample projects and added the following to a CPP file. The AutoSeededRandomPool is just a Crypto++ random number generator object (there's nothing special or tricky below).

// CPP file

#ifdef __cplusplus
extern "C" {
#endif

int __attribute__((weak)) rand(void)
{
    int r;

    AutoSeededRandomPool& prng = GetPRNG();
    prng.GenerateBlock(&r, sizeof(r));

    return r;
}

#ifdef __cplusplus
}
#endif

尝试编译它会导致 重新定义 int rand().我还尝试了以下方法:

Trying to compile it results in redefinition of int rand(). I've also tried the following:

// CPP file

#ifdef __cplusplus
extern "C" {
#endif

int rand(void) __attribute__((weak));

int random(void)
{
   ...
}

#ifdef __cplusplus
}
#endif

并且将 int rand(void) __attribute__((weak)); 移动到 H 文件会产生相同的 重新定义 int rand().

And moving int rand(void) __attribute__((weak)); to the H file produces the same redefinition of int rand().

而且我没有收到任何关于未知属性的错误或警告.

And I don't receive any errors or warnings about an unknown attribute.

我还看到 __GXX_WEAK__ 在预处理器中被定义为 1 ,但是 SUPPORTS_WEAKnot 定义的,所以它的混合信号(可能是一个错误,类似于 Define GXX_WEAK 使用 -fno-weak 时为 0).

I also see that __GXX_WEAK__ is defined to 1 in the preprocessor, but SUPPORTS_WEAK is not defined, so its mixed signals (perhaps a bug, similar to Define GXX_WEAK to 0 when using -fno-weak).

我不确定我是否做错了什么,或者遇到类似 const and weak attribute with c++ code, 或其他东西.

I'm not sure if I am doing something wrong, or experiencing something like const and weak attribute with c++ code, or something else.

Android 是否支持弱符号?如果是这样,如何使用它们.

Does Android support weak symbols? If so, how does one use them.

这里有一个没有答案的类似 Stack Overflow 问题:

Here a similar Stack Overflow question that does not have an answer:

一些系统细节:

  • 基本系统为 Mac OS X 10.8.5,已全面修补
  • Eclipse 4.4.1 (Luna),已全面修补
  • Android NDK 修订版 10d
  • GCC 4.9 交叉编译器

推荐答案

Android 不支持弱符号覆盖.

Android doesn't support weak symbol override.

在最近发布的 android-5.0.2_r1 中,请参阅 链接器中第 539 行的注释.cpp 源码

In the recent release android-5.0.2_r1, see the comment at line 539 in linker.cpp source code

/*
 *
 * Notes on weak symbols:
 * The ELF specs are ambigious about treatment of weak definitions in
 * dynamic linking.  Some systems return the first definition found
 * and some the first non-weak definition.   This is system dependent.
 * Here we return the first definition found for simplicity.
 */

此评论存在于版本 2.2_r1(位于 linker.c) 到最新版本 5.0.2_r1

This comment exists from version 2.2_r1 (which is in linker.c) to newest version 5.0.2_r1

这篇关于Android 是否支持弱符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆