0

This question is one that has occurred before on SO. For instance this question: Cmake doesn't find Boost But the answers there and elsewhere don't seem to work.

On Ubuntu 16.04 with the stock boost 1.58.0 installed, I have also built, in a custom location, boost 1.68.0.

Now I am trying to compile a simple c++ program using boost, with cmake. It does not find boost. Either version (although 1.68.0 is the one I really want to use). It gives: -- Could NOT find Boost (missing: Boost_DIR)

The CMakeLists.txt file is below. CMake 3.12.1 is being used.

cmake_minimum_required(VERSION 3.0.0)
set(CMAKE_CXX_STANDARD 17)

project(mytest CXX)

set(Boost_DEBUG ON)
set(Boost_DETAILED_FAILURE_MSG ON)
set(BOOST_ROOT /home/hal/projects/boost/boost)
# set(Boost_DIR /home/hal/projects/boost/boost)
#set(Boost_USE_DEBUG_LIBS       ON)
#set(Boost_USE_STATIC_LIBS        ON)
#set(Boost_USE_MULTITHREADED      ON)
# set(Boost_USE_STATIC_RUNTIME    OFF)
#set(Boost_INCLUDE_DIR /home/hal/projects/boost/boost )
set(Boost_ADDITIONAL_VERSIONS "1.58" "1.58.0")
#set(BOOST_INCLUDEDIR /home/hal/projects/boost/boost/include )
#set(BOOST_LIBRARYDIR /home/hal/projects/boost/boost/lib )
#SET(CMAKE_INCLUDE_PATH ${CMAKE_INCLUDE_PATH} "/home/hal/projects/boost/boost")
#SET(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} "/home/hal/projects/boost/boost/lib")
find_package(Boost  1.68.0   COMPONENTS system date_time  PATHS /home/hal/projects/boost/boost )

if(Boost_FOUND)
     include_directories(${Boost_INCLUDE_DIRS})
     add_executable(mytest main.cpp)
     target_link_libraries(mytest ${Boost_LIBRARIES} stdc++)
endif()
alangh
  • 63
  • 6
  • 1
    With `Boost_DEBUG` option CMake outputs where it searches Boost and which exact files are searched. Show that output. – Tsyvarev Sep 08 '18 at 18:29
  • Where is that output placed in the file system? – alangh Sep 08 '18 at 18:50
  • Just re-run `cmake` again, the messages are output into console. – Tsyvarev Sep 08 '18 at 19:09
  • When I run:cmake . | tee output.txt -- Could NOT find Boost (missing: Boost_DIR) -- Configuring done -- Generating done -- Build files have been written to: /home/hal/projects/testcpp/src – alangh Sep 08 '18 at 19:28
  • When I run cmake, or later, make - there does not seem to be any additional detailed debugging info that is outputted, and that may be part of the problem. – alangh Sep 08 '18 at 19:31
  • Well, so show exact output you got (add it to the question post via [edit] it). – Tsyvarev Sep 08 '18 at 19:41
  • 1
    Oh, I got it - remove `PATHS` option to the `find_package(Boost)`. The option does not do what you want, setting `BOOST_ROOT` should be sufficient. – Tsyvarev Sep 08 '18 at 19:44
  • That did it! It was the PATHS option. With that removed suddenly the debug info is shown, and it finds the 1.68 version of boost. Write it up as an answer and I'll check it. – alangh Sep 08 '18 at 21:23

1 Answers1

1

Unless you work with implementing of searching packaging, option PATHS for find_package is not such useful, just remove it:

find_package(Boost 1.68.0 COMPONENTS system date_time)

Explanations

There are two ways for search packages in CMake:

  1. With XXXConfig.cmake script, shipped with a specific package installation. In this script all paths (libraries, include directories, etc.) are hardcoded.
  2. With FindXXX.cmake script, shipped with CMake itself. This script searches the libraries and headers under the system directories (like /usr/local/lib) but also takes hints from the user.

By default, the second way is tried; only if FindXXX.cmake script is absent, the first way is used.

But some options for find_package are applied only for the first way, and PATHS is exactly an option of this sort: it specifies paths where XXXConfig.cmake file can be found. With such options, find_package uses the second way - it tries to find XXXConfig.cmake script and execute it. But it seems that your Boost installation lacks of this config script, so CMake fails to find Boost.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153