Search code examples
javac++tomcatjava-native-interfaceshared-libraries

JNI, error when using two .so files where one needs the other


I have two .so files - A.so and B.so, where A.so needs B.so.

I use System.setProperty("java.library.path","thePath");

and I check System.out.println(System.getProperty("java.library.path"));

Then I load A.so using System.load("/absolutePath/A.so");

A.so and B.so are in the same directory.

The error I get is that B.so can not find.

I can not use LD_LIBRARY_PATH.

ldd A.so shows B.so => ./B.so

I also use System.loadLibrary("A"); but the error is that A can not find.


Solution

  • The only solution may be:

    Let ldd shows the absolute path.

    Edit the CMakeList.txt file:

    LINK_DIRECTORIES("/the/absolute/path/")
    
    target_link_libraries(A B.so)
    

    UPDATE:

    Find the way, I only give the whole cmake file example.

    cmake_minimum_required(VERSION 3.12)
    project(xgbtSimiCalcer)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x -fpermissive -m64 -Wl,--no-as-needed -O3 -march=native")
    
    SET(xgbtSimiCalcer_SRC test0.cpp ../test1.cpp)
    
    INCLUDE_DIRECTORIES(./)
    INCLUDE_DIRECTORIES(../)
    
    SET(CMAKE_INSTALL_RPATH "\\$ORIGIN")
    LINK_DIRECTORIES("${CMAKE_SOURCE_DIR}/../../model/")
    LINK_LIBRARIES("${CMAKE_SOURCE_DIR}/../../test.so")
    ADD_LIBRARY(xgbtSimiCalcer SHARED ${xgbtSimiCalcer_SRC})
    TARGET_LINK_LIBRARIES(xgbtSimiCalcer m.so dl.so pthread.so)
    SET(LIBRARY_OUTPUT_PATH "../../tmp")
    SET(INSTALL_DIR "${CMAKE_SOURCE_DIR}/../../model_")
    INSTALL(TARGETS xgbtSimiCalcer LIBRARY DESTINATION ${INSTALL_DIR})