简体   繁体   中英

CMake Error at FindPackageHandleStandardArgs.cmake:137 (message): Could NOT find Boost (missing: regex) (found suitable version "1.72.0",

I downloaded the boost_1_72_0.tar.gz from the official site and unzipped it into my downloads folder at /USERS/macuser/Downloads/boost_1_72_0

I keep getting the following error when trying to link a simple .cpp file with Boost::regex

-- The C compiler identification is AppleClang 11.0.0.11000033
-- The CXX compiler identification is AppleClang 11.0.0.11000033
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
CMake Warning at /usr/local/Cellar/cmake/3.15.4/share/cmake/Modules/FindBoost.cmake:1144 (message):
  New Boost version may have incorrect or missing dependencies and imported
  targets
Call Stack (most recent call first):
  /usr/local/Cellar/cmake/3.15.4/share/cmake/Modules/FindBoost.cmake:1266 (_Boost_COMPONENT_DEPENDENCIES)
  /usr/local/Cellar/cmake/3.15.4/share/cmake/Modules/FindBoost.cmake:1904 (_Boost_MISSING_DEPENDENCIES)
  examples/HelloBoost/CMakeLists.txt:27 (find_package)


CMake Error at /usr/local/Cellar/cmake/3.15.4/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:137 (message):
  Could NOT find Boost (missing: regex) (found suitable version "1.72.0",
  minimum required is "1.72.0")
Call Stack (most recent call first):
  /usr/local/Cellar/cmake/3.15.4/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:378 (_FPHSA_FAILURE_MESSAGE)
  /usr/local/Cellar/cmake/3.15.4/share/cmake/Modules/FindBoost.cmake:2161 (find_package_handle_standard_args)
  examples/HelloBoost/CMakeLists.txt:27 (find_package)

My CMakeLists.txt file looks like below:

cmake_minimum_required(VERSION 3.1)

set (CMAKE_CXX_STANDARD 11)
set (PROJECT "Hello-Boost")

# headers
include_directories(include)

# sources
file(GLOB SOURCES "src/*.cpp")


# BOOST Configuration

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

set(Boost_INCLUDE_DIR /USERS/macuser/Downloads/boost_1_72_0/boost)
set(Boost_LIBRARY_DIR /USERS/macuser/Downloads/boost_1_72_0/libs)

find_package(Boost 1.72.0 COMPONENTS regex REQUIRED)

if(Boost_FOUND)
    message("boost lib: ${Boost_LIBRARIES}")
    message("boost inc:${Boost_INCLUDE_DIR}")
emdif()

include_directories(${Boost_INCLUDE_DIR})
link_directories(${Boost_LIBRARY_DIR})

# executable target
add_executable(${PROJECT} ${SOURCES})

# link boost
target_link_libraries(${PROJECT} PUBLIC Boost::regex)

My question is simple:
What in God's name is going wrong with the Cmake? Why on Earth it cannot find the darn boost library when millions of other human beings have successfully done so?

Thankyou

Before using Boost::regex you need to built Boost because it is not a header only library (as the majority of the other Boost libraries). This is usually done during installation via a package manager like homebrew or macports.

Header only libraries do not need to be added to the COMPONENTS section of your find_package call.

Then you do a

find_package(BOOST 1.72.0 COMPONENTS regex REQUIRED)

as you already did and later add the libraries to your target either by

target_link_libraries(${PROJECT} PUBLIC Boost::regex)

If you intend to use other Boost libraries later (header only or not), you should either name them explicitly in the target_link_libraries call or add

target_link_libraries(${PROJECT} PUBLIC ${Boost_LIBRARIES})

This will also add the Boost directory to your include directories. You can then omit the following lines

include_directories(${Boost_INCLUDE_DIR})
link_directories(${Boost_LIBRARY_DIR})

If you'd rather like to use the static libraries instead of the dynamic ones use

set(Boost_USE_STATIC_LIBS ON) 

before your find_package call.

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