Linking Boost Library in Linux

Linking Boost Library in Linux

Change -llibboost_system to -lboost_system.

In linux, the "lib" prefix in front of a library is not used when referencing said library.

How do I link boost to my program?


Answer

boost::system must be linked explicitly. In contrast to many other parts of boost, boost system is not header-only. Thus, you must make sure to link it when you compile. You have two options for linking.

Reference: http://www.boost.org/doc/libs/1_47_0/more/getting_started/unix-variants.html#link-your-program-to-a-boost-library

Option 1

Use -lboost_system (or the equivalent file in your /boost_####/stage/lib/ directory). Of course, you also have to set the library path using -L/file/path/to/libraries unless boost system resides in a standard lookup dir.

Example



g++ playground.cc -o playground -L~/boost/stage/lib/ -libboost_filesystem-mgw48-mt-1_54.a

Option 2

Include the full file path to the library at the end of your code.

Example

Run this from the command line. The triple quotes """ are necessary only for paths that contain spaces.

g++ playground.cc -o playground """C:\My Programs\boost_1_54_0\stage\lib\libboost_filesystem-mgw48-mt-1_54.a""" """C:\My Programs\boost_1_54_0\stage\lib\libboost_system-mgw48-mt-1_54.a"""

Note: For a list of files that are not header-only, see http://www.boost.org/doc/libs/1_47_0/more/getting_started/windows.html#header-only-libraries (Same link as in the first paragraph on "header-only").

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.

How to auto-link boost libraries with CMake

Boost docs:

Auto-Linking

Most Windows compilers and linkers have so-called “auto-linking support,” which eliminates the second challenge. Special code in Boost header files detects your compiler options and uses that information to encode the name of the correct library into your object files; the linker selects the library with that name from the directories you've told it to search.

The GCC toolchains (Cygwin and MinGW) are notable exceptions; GCC users should refer to the linking instructions for Unix variant OSes for the appropriate command-line options to use.

Note that the auto-linking feature has been known to fail on occasion (e.g. when your Boost libraries are installed using a non-standard setting). You can define BOOST_ALL_NO_LIB to disable the feature on Windows.

You shouldn't be hard-coding Boost paths into your CMakeLists.txt, though. Better use the platform-independent find_package:

set( Boost_USE_STATIC_LIBS OFF )
set( Boost_USE_MULTITHREADED ON )
set( Boost_USE_STATIC_RUNTIME OFF )

find_package( Boost 1.72.0 COMPONENTS thread fiber context )

if ( Boost_FOUND )
include_directories( ${Boost_INCLUDE_DIRS} )
link_libraries( learn_asio ${Boost_LIBRARIES} )
else()
message( FATAL_ERROR "Required Boost packages not found. Perhaps add -DBOOST_ROOT?" )
endif()

Linking boost in CLion project

I found the whole story, and I'm writing down the solution in case someone a little lost like me needs it.

So if you want to link boost to a project on windows using cmake with mingw as compiler, first you need to download and install mingw, with posix threads, and of cours x86_64 architecture. Then you download the boost sources and you can follow this tutorial in order to build Boost. Building Boost is mandatory for most of the boost libraries. By the way, to add things to windows PATH you need to go to Control Panel > System and Security > System > Advanced parameters > Environment variables and there you edit the PATH variable (you might want to add the mingw/bin folder to PATH). The last step will take a somewhat long time.

Once you have build your libraries, what you want to do is to link them on your CMakeLists.txt file. This is the code you want to add to the file :

set(Boost_ARCHITECTURE -x64)
set(BOOST_ROOT <path/to/boost>)
set(BOOST_INCLUDEDIR <path/to/boost/includes>)
set(BOOST_LIBRARYDIR <path/to/boost/lib>)
set(BOOST_NO_SYSTEM_PATHS ON)
find_package(Boost COMPONENTS <the components you want to load>)
include_directories(${Boost_INCLUDE_DIRS})

add_executable(your_project main.cpp)

target_link_libraries(untitled ${Boost_LIBRARIES})

So there is something up there that you won't find on FindBoost doc which is the Boost_ARCHITECTURE variable, which for some reason is not specified. The thing is that the lib files produced by the boost build will be named like this :
libboost_atomic-mgw81-mt-x64-1_71.a, but by default FindBoost will look for names such as boost_atomic-mgw81-mt-1_71, which it will never find since all the names have the architecture in it. So to enable FindBoost to search for .a files with -x64 (or -x32) in their name you need to add the set(Boost_ARCHITECTURE -x64) line, otherwise cmake will tell you that it did not find Boost.

Linking boost::asio using terminal

To find a package in Arch Linux, do:

sudo pacman -Ss boost

This will list packages with the string boost. Or, you can look up on the package website: https://www.archlinux.org/packages/extra/x86_64/boost/

One thing you should understand about boost is that a majority of its modules are header-only; if the linker complains about undefined references then you would have to link the required files. To link boost-asio, you would do

g++ -lboost-system <source> <exe>


Related Topics



Leave a reply



Submit