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

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

问题描述

我一直在寻找这样的问题:

I've been looking at questions like:

  • Cannot load library: reloc_library[1285]: cannot locate 'rand'
  • Android app crashes in the start because java.lang.UnsatisfiedLinkError

在我看来,这可能与弱符号的解决。也就是说,本机组件可以提供像兰特符号,但饰品它们与 __属性__((弱))。如果符号在另一个库中发现,类似的标准运行,那么弱链接符号会的没有的使用。另一方面,如果码元被缺失,则本机组件的版本将被使用。

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文件。该 AutoSeededRandomPool 只是一个加​​密++随机数生成器对象(有没有什么特别的或以下技巧)。

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兰特的重新定义()。我也试过如下:

// CPP file

#ifdef __cplusplus
extern "C" {
#endif

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

int random(void)
{
   ...
}

#ifdef __cplusplus
}
#endif

和移动 INT兰特(无效)__attribute __((弱)); 在H文件产生相同的 INT兰特的重新定义()

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 在preprocessor,但 SUPPORTS_WEAK 没有的定义,所以它的混合信号(可能是一个错误,类似的使用-fno弱时定义的 GXX_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).

我不知道如果我做错了什么,或遇到类似常量和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.

下面一个类似的堆栈溢出的问题是没有一个答案:

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

  • Different behavior of override weak function in shared library between OS X and Android

某些系统的细节:

  • 在基本系统为Mac OS X 10.8.5,完全补丁
  • 的Eclipse 4.4.1(LUNA),完全补丁
  • 的Andr​​oid NDK版本10D
  • 在GCC 4.9交叉编译器

推荐答案

Android不支持弱符号替代。

Android doesn't support weak symbol override.

在最近发布的Andr​​oid 5.0.2_r1, 看到接头处的注释行539。 CPP 源$ C ​​$ C

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天全站免登陆