共享库可以调用另一个共享库吗?

时间:2022-09-18 12:26:19

Can one shared library load and call functions from another shared library?

从另一个共享库中可以共享库负载和调用函数吗?

I have Shared library libDsmTestLib.so that use another shared libraries libDsmShared.so and libPINDsmShared.so

我有共享库libDsmTestLib。因此,使用另一个共享库libDsmShared。所以libPINDsmShared.so

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE           := DsmTestLib
LOCAL_SRC_FILES        := DSM_Library.cpp

LOCAL_LDLIBS := -lDsmShared
LOCAL_LDLIBS += -lPINDsmShared

include $(BUILD_SHARED_LIBRARY)

when I create libDsmTestLib.so and want to use it in my android java application like this:

当我创建libDsmTestLib。我想在我的android java应用程序中使用它:

package com.dsm;

import android.app.Activity;
import android.os.Bundle;

public class dsmTest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    static {
        try {
            System.loadLibrary("DsmTestLib");
        }
        catch( UnsatisfiedLinkError e ) {
             System.err.println("Native code library failed to load.\n" + e);
        }
    }  
}

In the catch block I get error

在catch块中,我得到错误。

Cannot load library: link_image[1962]: 33 could not load needed library 'libDsmShared.so' for 'libDsmTestLib.so' (load_library[1104]: Library 'libDsmShared.so' not found)

不能加载库:link_image[1962]: 33不能加载需要的库的libDsmShared。所以对libDsmTestLib。所以'(load_library[1104]:libDsmShared图书馆”。所以没有找到)

Loadlibrary function cant find library libDsmShared.so that uses my main library libDsmTestLib.so, Who can tell why ? and what can I do to solve this problem ?

Loadlibrary函数无法找到库libDsmShared。这就使用了我的主库libDsmTestLib。那么,谁能说出为什么呢?我能做些什么来解决这个问题呢?


Additional Information

I had a static library (.so written in C++) with functionality which I want to use from my Java android application, for that I create .cpp and .h files in which I call the function from the previously created library.

我有一个静态库(。用c++编写的功能,我想从我的Java android应用程序中使用,因为我创建了。cpp和。h文件,其中我调用了之前创建的库中的函数。

7 个解决方案

#1


7  

I found solution in this way - explicity loading libraries:

我通过这种方式找到了解决方案——解释加载库:

    static {
    try {
        System.loadLibrary("DsmShared");
        System.loadLibrary("DsmTestLib");
    }
    catch( UnsatisfiedLinkError e ) {
         System.err.println("Native code library failed to load.\n" + e);
    }
} 

#2


2  

Should be:

应该是:

LOCAL_LDLIBS += -L$(LOCAL_PATH)/libs/
LOCAL_LDLIBS += -lutils

Also don't forget copying libutils.so into your libs/armeabi folder

也不要忘记复制libutils。所以进入你的libs/armeabi文件夹。

#3


2  

I had the same error . This is how i solved it and maybe you should try this method.

我犯了同样的错误。这是我解决它的方法,也许你应该试试这个方法。

LOCAL_LDLIBS += -L$(LOCAL_PATH)/libs/libutils.so

LOCAL_LDLIBS + = - l(LOCAL_PATH)/ libs / libutils.so美元

#4


1  

No, Android VM will search the so file in its own file systems, but not a folder as [C:/cygwin/home/android-ndk-r5b/samples/testingDsm/lib] (I think).

不,Android VM将在自己的文件系统中搜索so文件,而不是像[C:/cygwin/home/ Android -ndk-r5b/samples/testingDsm/lib]那样的文件夹(我认为)。

It will search for it from the @androidvm:/system, or other folders specified by java.library.path.

它将从@androidvm:/系统或java.library.path指定的其他文件夹中搜索它。

#5


0  

FYI, I just discovered after a long debugging session that the order in which load lib does matter.

FYI,我刚刚在调试过程中发现了load lib的顺序。

    System.loadLibrary("libDsmShared");
    System.loadLibrary("libPINDsmShared");
    System.loadLibrary("DsmTestLib");

#6


-1  

  1. First, your Activity have to load all the shared libraries

    首先,您的活动必须加载所有共享库。

    static {
        try {
            System.loadLibrary("libDsmShared");
            System.loadLibrary("libPINDsmShared");
            System.loadLibrary("DsmTestLib");
        }
        catch( UnsatisfiedLinkError e ) {
            System.err.println("Native code library failed to load.\n" + e);
        }
    }
    
  2. Include the "lib*.so" when compiling your native code (in the Android.mk) as in

    包括“**。因此,在编译您的本机代码时(在Android.mk中)。

    ...
    LOCAL_LDLIBS := -L/cygdrive/home/android-ndk-r5b/samples/testingDsm/lib/libDsmShared -lDsmShared
    LOCAL_LDLIBS += -L/cygdrive/home/android-ndk-r5b/samples/testingDsm/lib/libPINDsmShared -lPINDsmShared
    ...
    

#7


-3  

I found this and test it:

The Android dynamic linker had a bug that prevented this from working, but was fixed in 1.6, I believe. If you use the NDK, use "LOCAL_SHARED_LIBRARIES := libB libC" when defining the libA module. This assumes that libB and libC are also NDK modules that were generated with the NDK.

Android动态链接器有一个bug,阻止了它的工作,但我相信它是固定在1.6的。如果使用NDK,在定义libA模块时使用“LOCAL_SHARED_LIBRARIES:= libB libC”。这假设libB和libC也是NDK模块,它们是由NDK生成的。

In case libB.so and libC.so are not generated with the NDK, you should do
the following:
  • in the libA module definition, use LOCAL_LDLIBS += /full/path/to/libB.so /full/path/to/libC.so this ensures that correct symbol exports are generated in libA.so

    在libA模块定义中,使用LOCAL_LDLIBS += /full/path/to/libB。所以/全/道路/ / libC。因此,这确保了在libaso中生成正确的符号导出。

  • manually copy libB.so and libC.so to $APP_PROJECT/libs/armeabi before rebuilding your .apk, this ensures that it will be copied to /data/data//lib at installation time by the package manager.

    手动复制libB。所以,libC。因此,在重新构建.apk之前,$APP_PROJECT/libs/armeabi将确保在安装时由包管理器将其复制到/数据/数据//lib。

now Android.mk have this look:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE           := DsmTestLib
LOCAL_SRC_FILES        := DSM_Library.cpp


#LOCAL_SHARED_LIBRARIES := DsmShared
#LOCAL_SHARED_LIBRARIES += PINDsmShared


# Set local libs with full path.                                                                
LOCAL_LDLIBS := C:/cygwin/home/android-ndk-r5b/samples/testingDsm/lib/libDsmShared.so           
LOCAL_LDLIBS += C:/cygwin/home/android-ndk-r5b/samples/testingDsm/lib/libPINDsmShared.so        

include $(BUILD_SHARED_LIBRARY)

but now error

Cannot load library: link_image[1962]: 33 could not load needed library 'C:/cygwin/home/android-ndk-r5b/samples/testingDsm/lib/libDsmShared.so' for 'libDsmTestLib.so' (load_library[1104]: Library 'C:/cygwin/home/android-ndk-r5b/samples/testingDsm/lib/libDsmShared.so' not found)

不能加载库:link_image[1962]: 33不能加载所需的库:/cygwin/home/ androidndk -r5b/samples/testingDsm/lib/libDsmShared。所以对libDsmTestLib。所以'(load_library[1104]:图书馆“C:/ cygwin / home / android-ndk-r5b /样本/ testingDsm / lib / libDsmShared。所以没有找到)

occurred, but when I check C:/cygwin/home/android-ndk-r5b/samples/testingDsm/lib/libDsmShared.so this path I found that the library exists there ... What's the metter ?

发生了,但是当我检查C:/cygwin/home/android-ndk-r5b/samples/testingDsm/lib/libDsmShared。所以这条路径我发现图书馆存在…满足是什么?

#1


7  

I found solution in this way - explicity loading libraries:

我通过这种方式找到了解决方案——解释加载库:

    static {
    try {
        System.loadLibrary("DsmShared");
        System.loadLibrary("DsmTestLib");
    }
    catch( UnsatisfiedLinkError e ) {
         System.err.println("Native code library failed to load.\n" + e);
    }
} 

#2


2  

Should be:

应该是:

LOCAL_LDLIBS += -L$(LOCAL_PATH)/libs/
LOCAL_LDLIBS += -lutils

Also don't forget copying libutils.so into your libs/armeabi folder

也不要忘记复制libutils。所以进入你的libs/armeabi文件夹。

#3


2  

I had the same error . This is how i solved it and maybe you should try this method.

我犯了同样的错误。这是我解决它的方法,也许你应该试试这个方法。

LOCAL_LDLIBS += -L$(LOCAL_PATH)/libs/libutils.so

LOCAL_LDLIBS + = - l(LOCAL_PATH)/ libs / libutils.so美元

#4


1  

No, Android VM will search the so file in its own file systems, but not a folder as [C:/cygwin/home/android-ndk-r5b/samples/testingDsm/lib] (I think).

不,Android VM将在自己的文件系统中搜索so文件,而不是像[C:/cygwin/home/ Android -ndk-r5b/samples/testingDsm/lib]那样的文件夹(我认为)。

It will search for it from the @androidvm:/system, or other folders specified by java.library.path.

它将从@androidvm:/系统或java.library.path指定的其他文件夹中搜索它。

#5


0  

FYI, I just discovered after a long debugging session that the order in which load lib does matter.

FYI,我刚刚在调试过程中发现了load lib的顺序。

    System.loadLibrary("libDsmShared");
    System.loadLibrary("libPINDsmShared");
    System.loadLibrary("DsmTestLib");

#6


-1  

  1. First, your Activity have to load all the shared libraries

    首先,您的活动必须加载所有共享库。

    static {
        try {
            System.loadLibrary("libDsmShared");
            System.loadLibrary("libPINDsmShared");
            System.loadLibrary("DsmTestLib");
        }
        catch( UnsatisfiedLinkError e ) {
            System.err.println("Native code library failed to load.\n" + e);
        }
    }
    
  2. Include the "lib*.so" when compiling your native code (in the Android.mk) as in

    包括“**。因此,在编译您的本机代码时(在Android.mk中)。

    ...
    LOCAL_LDLIBS := -L/cygdrive/home/android-ndk-r5b/samples/testingDsm/lib/libDsmShared -lDsmShared
    LOCAL_LDLIBS += -L/cygdrive/home/android-ndk-r5b/samples/testingDsm/lib/libPINDsmShared -lPINDsmShared
    ...
    

#7


-3  

I found this and test it:

The Android dynamic linker had a bug that prevented this from working, but was fixed in 1.6, I believe. If you use the NDK, use "LOCAL_SHARED_LIBRARIES := libB libC" when defining the libA module. This assumes that libB and libC are also NDK modules that were generated with the NDK.

Android动态链接器有一个bug,阻止了它的工作,但我相信它是固定在1.6的。如果使用NDK,在定义libA模块时使用“LOCAL_SHARED_LIBRARIES:= libB libC”。这假设libB和libC也是NDK模块,它们是由NDK生成的。

In case libB.so and libC.so are not generated with the NDK, you should do
the following:
  • in the libA module definition, use LOCAL_LDLIBS += /full/path/to/libB.so /full/path/to/libC.so this ensures that correct symbol exports are generated in libA.so

    在libA模块定义中,使用LOCAL_LDLIBS += /full/path/to/libB。所以/全/道路/ / libC。因此,这确保了在libaso中生成正确的符号导出。

  • manually copy libB.so and libC.so to $APP_PROJECT/libs/armeabi before rebuilding your .apk, this ensures that it will be copied to /data/data//lib at installation time by the package manager.

    手动复制libB。所以,libC。因此,在重新构建.apk之前,$APP_PROJECT/libs/armeabi将确保在安装时由包管理器将其复制到/数据/数据//lib。

now Android.mk have this look:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE           := DsmTestLib
LOCAL_SRC_FILES        := DSM_Library.cpp


#LOCAL_SHARED_LIBRARIES := DsmShared
#LOCAL_SHARED_LIBRARIES += PINDsmShared


# Set local libs with full path.                                                                
LOCAL_LDLIBS := C:/cygwin/home/android-ndk-r5b/samples/testingDsm/lib/libDsmShared.so           
LOCAL_LDLIBS += C:/cygwin/home/android-ndk-r5b/samples/testingDsm/lib/libPINDsmShared.so        

include $(BUILD_SHARED_LIBRARY)

but now error

Cannot load library: link_image[1962]: 33 could not load needed library 'C:/cygwin/home/android-ndk-r5b/samples/testingDsm/lib/libDsmShared.so' for 'libDsmTestLib.so' (load_library[1104]: Library 'C:/cygwin/home/android-ndk-r5b/samples/testingDsm/lib/libDsmShared.so' not found)

不能加载库:link_image[1962]: 33不能加载所需的库:/cygwin/home/ androidndk -r5b/samples/testingDsm/lib/libDsmShared。所以对libDsmTestLib。所以'(load_library[1104]:图书馆“C:/ cygwin / home / android-ndk-r5b /样本/ testingDsm / lib / libDsmShared。所以没有找到)

occurred, but when I check C:/cygwin/home/android-ndk-r5b/samples/testingDsm/lib/libDsmShared.so this path I found that the library exists there ... What's the metter ?

发生了,但是当我检查C:/cygwin/home/android-ndk-r5b/samples/testingDsm/lib/libDsmShared。所以这条路径我发现图书馆存在…满足是什么?