How to Link to Dynamic Boost Libs

Dynamic linking boost libraries and the auto-link not working properly?

From inside visual studio: go to your project and right click it to get the context menu. Go to properties. In the properties dialog go to:

Configuration Properties->C/C++->Preprocessor->Preprocessor Definitions

Try putting BOOST_ALL_DYN_LINK (or BOOST_THREAD_USE_DLL) in the Preprocessor Definitions list.

Then rebuild your demo.

Dynamic Linking to Boost DLLs

As far as I recall:

  1. Even to link DLL you need LIB file, this LIB file is produced along with DLL, and contains references to the DLL.

  2. Static linking with some boost libraries won't work in C++/CLI because they will rely on static TLS (thread local storage). Boost.Thread known to have this dependency, and Boost.Log probably uses Boost.Thread. If you have single-threaded code, you may try BOOST_LOG_NO_THREADS.

How can I optionally link against static or dynamic boost library using CMake?

To force the FindBoost CMake module to search for the desired libraries again, you have to clear the cache variables Boost_INCLUDE_DIR and Boost_LIBRARY_DIRS, i.e.:

set(Boost_USE_STATIC_LIBS ${USE_STATIC_BOOST})
set(Boost_USE_MULTITHREADED ON)
unset(Boost_INCLUDE_DIR CACHE)
unset(Boost_LIBRARY_DIRS CACHE)
find_package(Boost REQUIRED COMPONENTS thread program_options system)
if(USE_STATIC_BOOST)
message(STATUS "Linking against boost static libraries")
else()
message(STATUS "Linking against boost dynamic libraries")
endif()

Note that the argument CACHE is necessary to make the unset command clear the variables in the cache.

Also note that once the option variable USE_STATIC_BOOST has been cached, you need to explicitly set the variable from the command line or edit the value in the cache file to make CMake notice the change:

cmake ../.. -DUSE_STATIC_BOOST=NO 

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

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.



Related Topics



Leave a reply



Submit