8

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.

Community
  • 1
  • 1
Viktor Apoyan
  • 10,655
  • 22
  • 85
  • 147
  • possible duplicate of [NDK - How to use a generated .so library in another project](http://stackoverflow.com/questions/5669220/ndk-how-to-use-a-generated-so-library-in-another-project) – Mark Ingram Jul 07 '11 at 09:47

7 Answers7

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);
    }
} 
Vladimir Berezkin
  • 3,580
  • 4
  • 27
  • 33
  • Thank you very much, that solved at leased my problem and is exactly what is written in the first Chapter of the Overview of the NDK-Documentation, but I forgot the first lines I read until I finally got my code to compile in the NDK without errors. (/docs/OVERVIEW.html respectively /documentation.html) – white_gecko Oct 02 '11 at 20:41
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

Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
Vlad Yarovyi
  • 709
  • 1
  • 8
  • 16
  • It's not -L it should be -l and you should use -lutils and not use the whole path. I'm looking at what people is doing by adding just $(LOCAL_PATH)/libs/libutils.so that for me is like statically linking the lib into your own project/library. – Gonzalo Aguilar Delgado Apr 10 '15 at 11:00
1

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");
Sid Sarasvati
  • 819
  • 9
  • 10
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).

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

Cosmin
  • 21,216
  • 5
  • 45
  • 60
cyoudatu
  • 11
  • 1
1

Should be:

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

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

John Conde
  • 217,595
  • 99
  • 455
  • 496
knightwang
  • 11
  • 1
-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

    ...
    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
    ...
    
Zennichimaro
  • 5,236
  • 6
  • 54
  • 78
-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.

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 ?

Kevin Parker
  • 16,975
  • 20
  • 76
  • 105
Viktor Apoyan
  • 10,655
  • 22
  • 85
  • 147