When linking, is there something between “grab what you need” and “grab all” (-Wl,--whole-archive)?

假装没事ソ 提交于 2019-12-05 10:50:10
Alex Cohn

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)

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)

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!