How to add Boost libraries to CMakeLists.txt?

I need to add Boost libraries to my CMakeLists.txt. How do you do this or how do you add this?

+100
boost cmake
Jul 11 '11 at 6:14
source share
6 answers

Put this in your CMakeLists.txt file (change any parameters from OFF to ON if you want):

 set(Boost_USE_STATIC_LIBS OFF) set(Boost_USE_MULTITHREADED ON) set(Boost_USE_STATIC_RUNTIME OFF) find_package(Boost 1.45.0 COMPONENTS *boost libraries here*) if(Boost_FOUND) include_directories(${Boost_INCLUDE_DIRS}) add_executable(progname file1.cxx file2.cxx) target_link_libraries(progname ${Boost_LIBRARIES}) endif() 

Obviously, you need to put the necessary libraries where I put *boost libraries here* . For example, if you use the filesystem and regex library, you should write:

 find_package(Boost 1.45.0 COMPONENTS filesystem regex) 
+149
Jul 11 2018-11-11T00:
source share

You can use find_package to search for available boost libraries. It rejects the Boost search to FindBoost.cmake , which is installed by default with CMake.

When searching for Boost, calling find_package() populate a lot of variables (check the FindBoost.cmake link). Among them are BOOST_INCLUDE_DIRS , Boost_LIBRARIES and Boost_XXX_LIBRARY variables, with XXX replaced by specific Boost libraries. You can use them to specify include_directories and target_link_libraries .

For example, suppose you need boost :: program_options and boost :: regex, you would do something like:

 find_package( Boost REQUIRED COMPONENTS program_options regex ) include_directories( ${Boost_INCLUDE_DIRS} ) add_executable( run main.cpp ) # Example application based on main.cpp # Alternatively you could use ${Boost_LIBRARIES} here. target_link_libraries( run ${Boost_PROGRAM_OPTIONS_LIBRARY} ${Boost_REGEX_LIBRARY} ) 

Some general tips:

  • When searching, FindBoost checks the environment variable $ ENV {BOOST_ROOT}. You can set this variable before calling find_package if necessary.
  • When you have several versions of the boost assembly (multi-threaded, static, shared, etc.), you can specify the desired configuration before calling find_package. Do this by setting some of the following variables to On : Boost_USE_STATIC_LIBS , Boost_USE_MULTITHREADED , Boost_USE_STATIC_RUNTIME
  • When searching for Boost on Windows, take care of automatic binding. Read the “NOTE for Visual Studio Users” link .
    • My advice is to disable auto-binding and use cmake dependency handling: add_definitions( -DBOOST_ALL_NO_LIB )
    • In some cases, you may need to explicitly indicate that dynamic Boost is being used: add_definitions( -DBOOST_ALL_DYN_LINK )
+73
Jul 11 '11 at 7:00
source share

Adapting @LainIwakura's answer to modern CMake syntax with imported targets:

 set(Boost_USE_STATIC_LIBS OFF) set(Boost_USE_MULTITHREADED ON) set(Boost_USE_STATIC_RUNTIME OFF) find_package(Boost 1.45.0 COMPONENTS filesystem regex) if(Boost_FOUND) add_executable(progname file1.cxx file2.cxx) target_link_libraries(progname Boost::filesystem Boost::regex) endif() 

Please note: now there is no need to specify include directories manually, since he already took care of the imported targets Boost::filesystem and Boost::regex .
regex and filesystem can be replaced with any additional libraries that you need.

+18
May 10 '17 at 7:04
source share

May this be useful to some people. I got a naughty error: undefined reference to the symbol "_ZN5boost6system15system_categoryEv" // usr / lib / x86_64-linux-gnu / libboost_system.so.1.58.0: error when adding characters: DSO is not on the command line and for some reason I was missing explicitly enable the "system" and "filesystem" libraries. So, I wrote these lines in CMakeLists.txt

These lines are written at the beginning before the creation of the project executable file, since at this stage we do not need to link the boost library to our project executable file.

 set(Boost_USE_STATIC_LIBS OFF) set(Boost_USE_MULTITHREADED ON) set(Boost_USE_STATIC_RUNTIME OFF) set(Boost_NO_SYSTEM_PATHS TRUE) if (Boost_NO_SYSTEM_PATHS) set(BOOST_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../../3p/boost") set(BOOST_INCLUDE_DIRS "${BOOST_ROOT}/include") set(BOOST_LIBRARY_DIRS "${BOOST_ROOT}/lib") endif (Boost_NO_SYSTEM_PATHS) find_package(Boost COMPONENTS regex date_time system filesystem thread graph program_options) find_package(Boost REQUIRED regex date_time system filesystem thread graph program_options) find_package(Boost COMPONENTS program_options REQUIRED) 

Now, at the end of the file, I wrote these lines, considering "KeyPointEvaluation" as the executable file of my project.

 if(Boost_FOUND) include_directories(${BOOST_INCLUDE_DIRS}) link_directories(${Boost_LIBRARY_DIRS}) add_definitions(${Boost_DEFINITIONS}) include_directories(${Boost_INCLUDE_DIRS}) target_link_libraries(KeyPointEvaluation ${Boost_LIBRARIES}) target_link_libraries( KeyPointEvaluation ${Boost_PROGRAM_OPTIONS_LIBRARY} ${Boost_FILESYSTEM_LIBRARY} ${Boost_REGEX_LIBRARY} ${Boost_SYSTEM_LIBRARY}) endif() 
+5
Jan 29 '18 at 10:06
source share

I agree with answers 1 and 2 . However, I prefer to specify each library separately. This simplifies the work of applications in large projects. However, there is a risk of mistyping variable names (case sensitive). In this case, there is no direct cmake error, but some problems with link builders are undefined later, which may take some time. Therefore, I use the following cmake function:

 function(VerifyVarDefined) foreach(lib ${ARGV}) if(DEFINED ${lib}) else(DEFINED ${lib}) message(SEND_ERROR "Variable ${lib} is not defined") endif(DEFINED ${lib}) endforeach() endfunction(VerifyVarDefined) 

In the above example, it looks like this:

 VerifyVarDefined(Boost_PROGRAM_OPTIONS_LIBRARY Boost_REGEX_LIBRARY) target_link_libraries( run ${Boost_PROGRAM_OPTIONS_LIBRARY} ${Boost_REGEX_LIBRARY} ) 

If I wrote "BOOST_PROGRAM_OPTIONS_LIBRARY", there would be an error caused by cmake and not much later triggered by the linker.

+2
Nov 09 '15 at 16:56
source share

Try how to say Boost documentation :

 set(Boost_USE_STATIC_LIBS ON) # only find static libs set(Boost_USE_DEBUG_LIBS OFF) # ignore debug libs and set(Boost_USE_RELEASE_LIBS ON) # only find release libs set(Boost_USE_MULTITHREADED ON) set(Boost_USE_STATIC_RUNTIME OFF) find_package(Boost 1.66.0 COMPONENTS date_time filesystem system ...) if(Boost_FOUND) include_directories(${Boost_INCLUDE_DIRS}) add_executable(foo foo.cc) target_link_libraries(foo ${Boost_LIBRARIES}) endif() 

Remember to replace foo with your project name and components with yours!

+1
Sep 06 '18 at 12:57
source share



All Articles