链接时,“获取所需内容"和“获取所需内容"之间是否有关联?和“全部抓取" (-Wl,-整体存档)? [英] When linking, is there something between "grab what you need" and "grab all" (-Wl,--whole-archive)?

查看:214
本文介绍了链接时,“获取所需内容"和“获取所需内容"之间是否有关联?和“全部抓取" (-Wl,-整体存档)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一些静态初始化代码的库,需要在main()之前运行.如果您只将所有翻译单元一起编译,那么一切都很好,但是如果我提供了一个静态库(.a文件)并让用户将其应用程序链接到它,则它不起作用-链接器只是忽略了这样做的符号我的静态初始化.

I have this library which involves some static initialization code which needs to run before main(). It all works well if you just compile all of the translation units together, but it doesn't work if I provide a static library (.a file) and have users link their application against it - the linker simply ignores the symbols which do my static initialization.

或者,如果我为GCC指定了-Wl,--whole-archive选项,即为GNU链接指定了--whole-archive选项,则可以使链接程序在静态库中提取所有内容.

Alternatively, I can make the linker pick up everything in the static library, if I specify the -Wl,--whole-archive option to GCC, i.e. specify the --whole-archive option to GNU link.

但是有中间立场吗?我可以标记一些符号并使链接程序始终为可执行文件选择它们,而其余符号仅在需要时添加吗?

But is there some middle ground? Can I mark some symbols and make the linker always pick them up for the executable, while the rest of the symbols are added only when needed?

动机:我使用一些静态块在工厂中注册类;我想让我的代码可以作为(非动态)库使用,而用户代码不必为要填充的工厂执行任何魔咒".

Motivation: I use some static blocks to register classes in a factory; I want to make my code available as a (non-dynamic) library, without the user code having to perform any "magic incantation" for the factory to be populated.

一些相关问题:

  • How to force include static objects from a static library in C++ (MSVC 11)
  • How to force gcc to link unreferenced, static C++ objects from a library
  • How to force gcc to link an unused static library

推荐答案

您可以强制链接程序保留给定的函数(自然而然,从该函数调用的所有代码).将-u my_function添加到链接命令.许多构建系统都允许静态库将构建设置导出"给使用它们的用户.例如,对于Android ndk-build 框架,您可以指定类似

You can force the linker to keep a given function (and naturally, all code that is called from this function). Add -u my_function to the link command. Many build systems let static libraries to 'export' the build settings to those who use them. E.g., for Android ndk-build framework, you can specify something like

include $(CLEAR_VARS)
LOCAL_MODULE := the_best_static_library
LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libfoo.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_EXPORT_LDFLAGS := -u my_function
include $(PREBUILT_STATIC_LIBRARY)

在模块 Android.mk 中.人们可以通过在其 Android.mk 中添加简单的语句

in your module Android.mk. People reuse it by adding to their Android.mk the simple statement

$(call import-module,third_party/the_best_static_library)

NB 对于这种方法,不能将my_function()声明为static.如果某个符号在文件范围内被声明为静态,则链接器可能根本猜不到它的名称.幸运的是,如果链接器决定保留某些代码中的引用,那么它也不会被剥离.此外,除非您进行特别努力,否则链接程序将剥离或保留整个编译单元(也称为C文件).因此,通常足够即可将虚拟函数锚定"为保留许多功能和数据.

N.B. For this approach to work, my_function() cannot be declared static. If some symbol is declared as static at the file scope, then the linker guess not know it by name at all. Luckily, if it is referenced in some code that the linker decides to keep, then it will also not get stripped off. Furthermore, unless you make a special effort, the linker will strip or keep whole compilation units (a.k.a. C files). Thus, it is usually enough to "anchor" a dummy function to keep many functions and data.

这篇关于链接时,“获取所需内容"和“获取所需内容"之间是否有关联?和“全部抓取" (-Wl,-整体存档)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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