Search code examples
c++boost

cmake find_package: why cannot find some components in boost


Find_package command is a nightmare to me. I am trying to include some specified components in boost into my project. Some components could not be found with find_package command for different error. Can anyone help to explain the error reported?

case 1:

cmake_minimum_required(VERSION 3.15)
project(tryBoost)

set(CMAKE_CXX_STANDARD 14)

set(BOOST_ROOT "D:\\cygwin64\\home\\yubo\\boost_1_62_0") # either set it here or from the command line
find_package(Boost 1.62.0 REQUIRED COMPONENTS json) # header only libraries must not be added here
add_executable(tryBoost main.cpp)

I try to find json, but error reported: No header defined for json; skipping header check

case 2:

cmake_minimum_required(VERSION 3.15)
project(tryBoost)

set(CMAKE_CXX_STANDARD 14)

set(BOOST_ROOT "D:\\cygwin64\\home\\yubo\\boost_1_62_0") # either set it here or from the command line
find_package(Boost 1.62.0 REQUIRED COMPONENTS system) # header only libraries must not be added here
add_executable(tryBoost main.cpp)

I try to find system, but error reported: Could NOT find Boost (missing: Boost_INCLUDE_DIR system)

How boost organizes its components in sub dirs? How find_package command works when scaning boost root dir? why "header only libraries must not be added here".

thanks.


Solution

  • Please use below CMakeList.txt for reference.

    cmake_minimum_required(VERSION 3.15)
    project(tryBoost)
    
    set(CMAKE_CXX_STANDARD 14)
    #set(Boost_DEBUG ON)
    
    set(Boost_USE_STATIC_LIBS ON)
    set(Boost_USE_MULTITHREADED ON)
    
    set(BOOST_ROOT "/home/yubo/boost_1_78_0")
    
    add_executable(tryBoost main.cpp)
    
    
    set(Boost_NO_BOOST_CMAKE FALSE CACHE BOOL "" FORCE)
    find_package(Boost 1.78.0 REQUIRED COMPONENTS json)
    message("${Boost_FOUND}")
    
    message("Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
    target_include_directories(tryBoost PUBLIC ${Boost_INCLUDE_DIRS})
    
    message("Boost_LIBRARY_DIRS: ${Boost_LIBRARY_DIRS}")
    message("Boost_LIBRARIES: ${Boost_LIBRARIES}")
    target_link_libraries(tryBoost  ${Boost_LIBRARIES})