覆盖静态库弱符号 [英] Override weak symbols in static library

查看:800
本文介绍了覆盖静态库弱符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要让我的项目的静态。一个库从多个来源,他们中的一些薄弱定义功能等实现它们。比方说,作为例子,我有:

lib1.c:

无效的DefaultHandler()
{
    对于(;;);
}
无效将myHandler()__attribute __((弱,别名(DefaultHandler的)));

lib2.c:

无效将myHandler()
{
    / *在这里做我的东西* /
}

然后我想将它们放到一个单一的库,所以,它似乎透明为终端应用

$ AR -r libhandlers.a lib1.o lib2.o

但现在有2个符号将myHandler 在libhandlers:

$纳米libhandlers.a | grep的将myHandler
00000001W¯¯将myHandler
00000581ŧ将myHandler

,然后使用LIB时,弱引用链接。唯一的解决办法我有一刻是在图书馆里不包括 lib2.c ,但将其添加为应用程序的Makefile源...这不是令人满意的,因为我想提供只使用一些库,而不是一大堆的文件。

- 全归档选项也不能让人满意,因为我嵌入式系统上工作,我不希望包括所有的事情我不需要<。 / p>

有没有办法来编译库,这样如果提供一个强大的弱符号消失?

请注意:我使用臂无 - EABI - 海合会V4.8


解决方案

这是那 .A 图书馆工作方式的副产品 - 他们只是集合的.o 文件。

在编译链接时会发生什么情况是,第一个提及的名字得到解决,以弱引用和强名称从来没有得到一看。

您可以通过实际使得无论是相同的名称和强大的测试这个自己,你会看到完全一样的行为。

如果你想先解决了强引用,然后把它们早些时候归档,或创建一个单独的强大的存档,链接,首先在连接线。

虽然不能直接适用于你的情况下,您使用的是嵌入式环境,对弱强引用生效正确创建/消费的.so 动态库时而不是 .A 档案。当你创建一个。所以,所有的弱引用该补库将不会产生错误,只有其中一人将被用于最终产品;如果有一个强定义的任何地方那么它被使用,而不是任何一个弱的人(如果这只是正常工作,当你创建一个的.so ,你链接所有的的.o 文件弥补单独,或使用 - 整个归档创建时的.so 如果链接到 .A )。

I want to make a static .a library for my project from multiple sources, some of them define weak functions and others implements them. Let's say as example I have :

lib1.c :

void defaultHandler()
{
    for(;;);
}
void myHandler() __attribute__((weak, alias ("defaultHandler")));

lib2.c :

void myHandler()
{
    /* do my stuff here */
}

Then I want to put them into one single library, so that it seems transparent for the end application

$ ar -r libhandlers.a lib1.o lib2.o

But there is now 2 symbols myHandler in libhandlers :

$ nm libhandlers.a | grep "myHandler"
00000001 W myHandler
00000581 T myHandler

And then when using the lib, the weak reference is linked. The only solution I have for the moment is to not include in the library lib2.c but to add it as source in the application's Makefile… that's not satisfying since I would like to provide only a few libraries to use and not a whole bunch of files.

The --whole-archive option is also not satisfying since I work on embedded system and I don't want to include all things I don't need.

Is there a way to compile the library so that the weak symbol disappear if a strong one is provided?

NOTE : I'm using arm-none-eabi-gcc v4.8

解决方案

This is a byproduct of the way that .a libraries work - they're simply a collection of .o files.

What happens at compile link time is that the first reference to the name gets resolved to the weak reference and the strong name never gets a look in.

You can test this yourself by actually making both the identical name and strong and you'll see exactly the same behaviour.

If you want the strong references resolved first, then put them earlier in the archive, or create a separate strong archive and link that first in the link-line.

While not directly applicable to your case, as you're using an embedded environment, weak vs. strong references come into force properly when creating/consuming .so dynamic libraries rather than .a archives. When you create a .so, all the weak references that make up the library will not generate an error, and only one of them will be used for the final product; and if there is a strong definition anywhere then it gets used rather than any of the weak ones (this only works properly if, when you're creating a .so, you link all the .o files that make it up separately, or use the --whole-archive when creating the .so if linking to a .a).

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

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