Override weak symbols in static library

后端 未结 1 1067
抹茶落季
抹茶落季 2020-12-17 21:54

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 :

1条回答
  •  囚心锁ツ
    2020-12-17 22:23

    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).

    0 讨论(0)
提交回复
热议问题