Mingjie Li Mingjie Li - 1 month ago 11
C++ Question

How could I link the Boost (not in usr/include) in CMakeList.txt using find_package

Now, I need to use

" find_package(Boost 1.62.0 COMPONENTS program_options serialization system filesystem thread REQUIRED)"
to include some libs into my program, but my boost library is installed in other directory, not in the defualt directory**(usr/include..)**. Now it occurs the following errors:

CMake Error at /usr/share/cmake/Modules/FindBoost.cmake:1138 (message):
Unable to find the requested Boost libraries.
Boost version: 1.41.0
Boost include path: /usr/includ
Detected version of Boost is too old.
Requested version was 1.62 (or
newer).


I give out some contents of my CMakelist.txt:

SET(BOOST_DIR /home/mingjli/folder_Mingjie/Software/boost_1.62.0/include/boost)
SET(BOOST_LIB /home/mingjli/folder_Mingjie/Software/boost_1.62.0/lib)
INCLUDE (${Source_Path}/IndexerLauncher.cmake NO_POLICY_SCOPE)
INCLUDE_DIRECTORIES(${BOOST_DIR})
LINK_DIRECTORIES(${BOOST_LIB})
INCLUDE_DIRECTORIES(${Source_Path})
ADD_EXECUTABLE (indexer_launcher ${IndexerLauncher})
TARGET_LINK_LIBRARIES (indexer_launcher nearest_search_lib)
target_link_libraries( indexer_launcher ${Boost_LIBRARIES} )


Thank you!!

Answer
Boost version: 1.41.0  
                 ^^

This means, that you have boost version 1.41.0 installed, but you are requesting 1.62.0. You can update boost or change minimal boost version requirements:

find_package(Boost 1.41.0 COMPONENTS program_options serialization system filesystem thread REQUIRED)
                     ^^

There is nothing really special you should have in your CMakeLists.txt to include Boost. Here is a sample that works for me:

cmake_minimum_required(VERSION 2.8)
find_package(Boost REQUIRED filesystem system)
include_directories(${Boost_INCLUDE_DIRS})
target_link_libraries(mytarget ${Boost_LIBRARIES})

UPDATE: If you can't update your system boost version, you can install boost into your home folder:

  1. download boost sources from www.boost.org
  2. compile and install new boost into your home directory:

    b2 toolset=gcc install --prefix=/home/user/boost

After boost is compiled - you can use it from /home/user/boost:

cmake .. -DBOOST_ROOT=/home/user/boost