【问题标题】:Preventing functions from being stripped from a static library when linked into a shared library?链接到共享库时,防止函数从静态库中剥离?
【发布时间】:2011-04-02 17:48:31
【问题描述】:

我有一个静态库 Foo,供共享库 Bar 使用。 Bar 是我的 Android 应用程序加载的本机共享库。 Foo 包含 JNI 函数,这些函数只能由 Java 代码调用,而不能由 Bar 中的任何 C++ 代码调用。因此,在构建共享库 (Bar) 时,这些 JNI 函数会从静态库 (Foo) 中删除。我目前正在使用一种稍微老套的方法来防止这种情况发生。

那么,在这种情况下,有没有办法告诉编译器在链接时不要剥离 JNI(或任何)函数?

【问题讨论】:

    标签: android java-native-interface


    【解决方案1】:

    他们没有被剥夺,他们被忽略了。当共享库被链接时,链接器只会拉入带有实际使用函数的目标文件。 (这就是定义静态库的工作方式。)

    我相信将“--whole-archive”标志传递给链接器会导致它从静态库中提取所有目标文件。您可以使用“-Wl,-whole-archive”在 gcc 链接行上提供它。在指定库之后,您需要使用“-Wl,-no-whole-archive”来跟随它,否则 ld 将继续它遇到的任何其他静态库的行为,这可能不是您想要的行为。另请参阅 Linux 系统上的 ld(1) 手册页。

    完成同样事情的另一种方法是输出一个巨大的 .o 文件而不是 .a 文件。

    编辑: 简单的命令行示例,在桌面上使用 libz:

    % echo "int main() { return 0; }" > foo.c
    % gcc -o foo /usr/lib/libz.a foo.c
    % ls -s foo
    12 foo*
    % gcc -o foo -Wl,-whole-archive /usr/lib/libz.a -Wl,-no-whole-archive foo.c
    % ls -s foo
    104 foo*
    

    (这里必须使用“/usr/lib/libz.a”而不是“-lz”,因为后者会找到共享库/usr/lib/libz.so。)

    我没有经常使用 NDK,但看起来将标志添加到 LOCAL_LDFLAGS 可能会奏效。

    【讨论】:

    • fadden - 对于我们中间的人 - 您能否发布一个快速示例,说明您需要如何设置 Android.mk 文件(至少与链接相关的两行)?
    【解决方案2】:

    让我们从基本的two-libs sample from NDK 开始。这是它的原始 Android.mk 文件:

     1 # Copyright (C) 2009 The Android Open Source Project
     2 #
     3 # Licensed under the Apache License, Version 2.0 (the "License");
     4 # you may not use this file except in compliance with the License.
     5 # You may obtain a copy of the License at
     6 #
     7 #      http://www.apache.org/licenses/LICENSE-2.0
     8 #
     9 # Unless required by applicable law or agreed to in writing, software
    10 # distributed under the License is distributed on an "AS IS" BASIS,
    11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12 # See the License for the specific language governing permissions and
    13 # limitations under the License.
    14 #
    15 
    16 # the purpose of this sample is to demonstrate how one can
    17 # generate two distinct shared libraries and have them both
    18 # uploaded in
    19 #
    20 
    21 LOCAL_PATH:= $(call my-dir)
    22 
    23 # first lib, which will be built statically
    24 #
    25 include $(CLEAR_VARS)
    26
    27 LOCAL_MODULE    := libtwolib-first
    28 LOCAL_SRC_FILES := first.c
    29 
    30 include $(BUILD_STATIC_LIBRARY)
    31 
    32 # second lib, which will depend on and include the first one
    33 #
    34 include $(CLEAR_VARS)
    35 
    36 LOCAL_MODULE    := libtwolib-second
    37 LOCAL_SRC_FILES := second.c
    38 
    39 LOCAL_STATIC_LIBRARIES := libtwolib-first
    40 
    41 include $(BUILD_SHARED_LIBRARY)
    

    请注意,JNI 函数位于 second.c 中,它不属于 libtwolib-first 静态库。

    首先,让我们重现您的问题。改变很简单:

    ...
    27 LOCAL_MODULE    := libtwolib-first
    28 LOCAL_SRC_FILES := first.c second.c
    ...
    36 LOCAL_MODULE    := libtwolib-second
    37 LOCAL_SRC_FILES :=
    

    如果运行修改后的项目,会报如下错误:

    E/AndroidRuntime( 4213): FATAL EXCEPTION: main
    E/AndroidRuntime( 4213): java.lang.UnsatisfiedLinkError: add
    E/AndroidRuntime( 4213):        at com.example.twolibs.TwoLibs.add(Native Method)
    E/AndroidRuntime( 4213):        at com.example.twolibs.TwoLibs.onCreate(TwoLibs.java:39)
    E/AndroidRuntime( 4213):        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    E/AndroidRuntime( 4213):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
    E/AndroidRuntime( 4213):        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    E/AndroidRuntime( 4213):        at android.app.ActivityThread.access$2300(ActivityThread.java:125)
    E/AndroidRuntime( 4213):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    E/AndroidRuntime( 4213):        at android.os.Handler.dispatchMessage(Handler.java:99)
    E/AndroidRuntime( 4213):        at android.os.Looper.loop(Looper.java:123)
    E/AndroidRuntime( 4213):        at android.app.ActivityThread.main(ActivityThread.java:4627)
    E/AndroidRuntime( 4213):        at java.lang.reflect.Method.invokeNative(Native Method)
    E/AndroidRuntime( 4213):        at java.lang.reflect.Method.invoke(Method.java:521)
    E/AndroidRuntime( 4213):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
    E/AndroidRuntime( 4213):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
    E/AndroidRuntime( 4213):        at dalvik.system.NativeStart.main(Native Method)
    

    您准确地解释了这个问题:链接器“剥离”了“未使用”的 Java_com_example_twolibs_TwoLibs_add() 条目。

    现在让我们解决这个问题:

    39 LOCAL_STATIC_LIBRARIES := libtwolib-first
    39 LOCAL_WHOLE_STATIC_LIBRARIES := libtwolib-first

    示例再次有效!

    【讨论】:

    • 谢谢!这正是我所需要的。
    【解决方案3】:

    对于那些使用CMakeLists.txt 的人,您可以这样做:

    target_link_libraries(
            # The shared library name,
            Bar
    
            # Since Foo is a static library, only the symbols used in Bar are kept. But
            # we intend to use other symbols from the java side, hence, we instruct the
            # linker to grab the whole library,
            -Wl,-whole-archive
            Foo
            -Wl,-no-whole-archive
    
            # Other libraries needed, potentially including some static libraries that 
            # will be treated the normal way,
            android
            ${log-lib})
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-09
      • 2015-10-08
      • 1970-01-01
      • 2010-12-23
      相关资源
      最近更新 更多