简体   繁体   中英

C++ How to link boost libraries with my shared library to work on the target machine?

I try to make jni shared library.

I use boost library in the code of this library.

I successfuly make shared library on the development machine and test's done.

I make boost lib folder which have libboost_*.so files in on target machine and add it to LD_LIBRARY_PATH.

So, I try to test on the target machine (same OS platform with development machine) but target machine cannot link it.

So, my java program find my .so (libsample.so) but libsample.so can't find boost library and throw message ( undefined symbol: _ZTIN5boost6detail16thread_data_baseE)

How can I solve this problem?

  1. I want to pack the boost libaries on my shared library
  2. or I want to dynamically linking with boost library with my shared libarary.

my project's CMakeLists.txt following

cmake_minimum_required(VERSION 3.0)
### This CMakeLists.txt  : Root CMake of this project
################## complie settings of this project ##################
set(ARTIFACT_NAME "sample-plugin")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread ")
add_definitions("-Wno-deprecated-declarations")
add_definitions("-Wno-write-strings")
################## Boost Settings ##################
set(Boost_NO_SYSTEM_PATH ON)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME ON)
set(BOOST_INCLUDE_DIR "${BOOST_ROOT}/boost")
include_directories(${BOOST_INCLUDE_DIR})
link_directories(${BOOST_INCLUDE_DIR})
link_directories(${BOOST_LIBRARY_DIR})
find_package(Boost 1.58.0 REQUIRED)
include_directories("$ENV{JAVA_HOME}/include")
if (WIN32)
include_directories("$ENV{JAVA_HOME}/include/win32")
else ()
include_directories("$ENV{JAVA_HOME}/include/linux")
endif ()
add_library(${ARTIFACT_NAME} SHARED ${SOURCES} )
target_link_libraries(${ARTIFACT_NAME} ${Boost_LIBRARIES})

Thanks, Martin Bonner.

I solve this problem.

Here is my edited CMakeLists.txt

 ################## complie settings of this project ################## set(ARTIFACT_NAME "sample-plugin") #set(CMAKE_CXX_STANDARD 11) # 아래에 -std=c++11 옵션과 중복 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -shared -fPIC -std=c++11 -pthread ") set(CMAKE_POSITION_INDEPENDENT_CODE ON) add_definitions("-Wno-deprecated-declarations") add_definitions("-Wno-write-strings") ################## Boost Settings ################## set(Boost_NO_SYSTEM_PATH ON) set(Boost_USE_STATIC_LIBS ON) set(Boost_USE_MULTITHREADED ON) set(Boost_USE_STATIC_RUNTIME ON) set(BOOST_INCLUDE_DIR "${BOOST_ROOT}/boost") include_directories(${BOOST_INCLUDE_DIR}) link_directories(${BOOST_INCLUDE_DIR}) link_directories(${BOOST_LIBRARY_DIR}) unset(Boost_INCLUDE_DIR CACHE) unset(Boost_LIBRARY_DIRS CACHE) find_package(Boost 1.58.0 REQUIRED COMPONENTS thread date_time filesystem system program_options ) ################## Target Settings ################## add_library(${ARTIFACT_NAME} SHARED ${SOURCES}) set_target_properties(${ARTIFACT_NAME} PROPERTIES POSITION_INDEPENDENT_CODE ON) target_link_libraries(${ARTIFACT_NAME} ${Boost_LIBRARIES}) 

Before this cmake run, I recompile boost libraries with -cxxflags=-fPIC because of this issue

I successfuly make .so library including boost libraries as static in it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM