简体   繁体   中英

How can I use Boost Log with CMake? Find_Package Error

I'm trying to use Boost log in a CMake project but I get an error when reloading CMake.

Here is my CMake:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(MyProject)
add_executable(MyProject)

FIND_PACKAGE(Boost COMPONENTS program_options system log filesystem REQUIRED)

if (Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS})

    LIST(APPEND CMAKE_CXX_FLAGS "-Wall -O3")

    TARGET_LINK_LIBRARIES(MyProject ${Boost_LIBRARIES}

endif ()

Everything was working mighty fine until I added log to FIND_PACKAGES()

Now, after having added log to FIND_PACKAGES , I get the following CMake output:

  Could not find a package configuration file provided by "boost_log"
  (requested version 1.71.0) with any of the following names:

    boost_logConfig.cmake
    boost_log-config.cmake

  Add the installation prefix of "boost_log" to CMAKE_PREFIX_PATH or set
  "boost_log_DIR" to a directory containing one of the above files.  If
  "boost_log" provides a separate development package or SDK, be sure it has
  been installed.

I cannot find those files (-config.cmake) for log anywhere and I don't know how to get them. I do have those files for program_options , system and filesystem packages.

I not sure about CMAKE_MINIMUM_REQUIRED(VERSION 2.8) . However, for what it's worth, I use:

cmake_minimum_required (VERSION 3.12)
cmake_policy(SET CMP0074 NEW)

see CMP0074 . Which may be a part of your problem.

In your place I would also use:

project (MyProject)

add_executable(${PROJECT_NAME} MyProjectMain.cpp}

target_compile_options(${PROJECT_NAME} PRIVATE -Wall -O3)

find_package(Boost REQUIRED COMPONENTS system filesystem program_options log_setup log)
if (Boost_FOUND)
    target_include_directories(${PROJECT_NAME} PUBLIC ${Boost_INCLUDE_DIRS})
    target_link_libraries(${PROJECT_NAME}
      PRIVATE
      Boost::system
      Boost::filesystem
      Boost::program_options
      Boost::log_setup
      Boost::log)

endif ()

This uses the relatively new target_* CMake instructions and works for me, see:CMakeLists.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM