问题
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
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:
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
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)
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 ?
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.
回答1:
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:
Should be:
LOCAL_LDLIBS += -L$(LOCAL_PATH)/libs/
LOCAL_LDLIBS += -lutils
Also don't forget copying libutils.so
into your libs/armeabi
folder
回答3:
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
回答4:
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).
It will search for it from the @androidvm:/system
, or other folders specified by java.library.path.
回答5:
FYI, I just discovered after a long debugging session that the order in which load lib does matter.
System.loadLibrary("libDsmShared");
System.loadLibrary("libPINDsmShared");
System.loadLibrary("DsmTestLib");
回答6:
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); } }
Include the "lib*.so" when compiling your native code (in the Android.mk) as in
... 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:
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.
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
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.
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)
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 ?
来源:https://stackoverflow.com/questions/6041934/can-shared-library-call-another-shared-library