# Checking if specific boost headers are available

**URL:** <https://discourse.cmake.org/t/checking-if-specific-boost-headers-are-available/2352>\
**Category:** Code\
**Created:** [December 11, 2020, 8:34pm UTC](https://discourse.cmake.org/t/checking-if-specific-boost-headers-are-available/2352 "2020-12-11T20:34:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![javierelpianista](https://discourse.cmake.org/user_avatar/discourse.cmake.org/javierelpianista/32/1022_2.png) [@javierelpianista](https://discourse.cmake.org/u/javierelpianista)\
**Post date:** [December 11, 2020, 8:34pm UTC](https://discourse.cmake.org/t/checking-if-specific-boost-headers-are-available/2352/1 "2020-12-11T20:34:22Z")

</div>

Is there a way to check if specific boost header-only libraries are installed? I am aware of the `COMPONENTS` option in the `find_package(Boost ...)` command, but it only works for compiled libraries.

I am particularly interested in checking if the user has `boost/multiprecision/mpc.hpp` and `boost/multiprecision/mpfr.hpp` installed.

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [December 12, 2020, 12:25am UTC](https://discourse.cmake.org/t/checking-if-specific-boost-headers-are-available/2352/2 "2020-12-12T00:25:50Z")

</div>

Checking `if (EXISTS "${Boost_INCLUDE_DIR}/…")` is probably the only reliable way. Having a component per header library would be nice. Their [issue tracker](https://github.com/boostorg/boost_install/issues) would be the place to ask though.

---

<div class="post-metadata">

**Author:** ![javierelpianista](https://discourse.cmake.org/user_avatar/discourse.cmake.org/javierelpianista/32/1022_2.png) [@javierelpianista](https://discourse.cmake.org/u/javierelpianista)\
**Post date:** [December 12, 2020, 3:24pm UTC](https://discourse.cmake.org/t/checking-if-specific-boost-headers-are-available/2352/3 "2020-12-12T15:24:28Z")

</div>

Thank you for your comment. It turns out I was tackling the problem in the wrong way. The reason I was looking for specific headers is that in Ubuntu 18.04, only `mpfr.hpp` is included, but the reason for this is that `mpc.hpp` is relatively new, so what I should have done is check if the Boost version is recent enough, which can be easily done with `find_package`.  
Still, before realizing that, I found a solution using the `check_include_file_cxx` macro, which I paste below from Stack Overflow for future reference.

The macro `check_include_file_cxx` not only checks if the required header is available, but also tries to compile a small test program. For that, it also needs to link to libraries (`-lmpfr` for `boost/multiprecision/mpfr.hpp` and `-lmpfr -lmpc` for `boost/multiprecision/mpc.hpp`).

This did the trick for me.

```auto
find_package(Boost REQUIRED
    VERSION 1.69)

if ( Boost_FOUND )
    include_directories(${Boost_INCLUDE_DIRS})
endif()

include(CheckIncludeFileCXX)
cmake_policy(SET CMP0075 NEW) # Required for specifying CMAKE_REQUIRED_LIBRARIES

set(CMAKE_REQUIRED_INCLUDES "${Boost_INCLUDE_DIRS}")
set(CMAKE_REQUIRED_LIBRARIES "mpfr" "mpc" "gmp")

set(REQUIRED_BOOST_HEADERS
    "boost/multiprecision/mpfr.hpp;boost/multiprecision/mpc.hpp"
    )
foreach(HEADER ${REQUIRED_BOOST_HEADERS})
    check_include_file_cxx("${HEADER}" ${HEADER}_FOUND)
    if (NOT ${HEADER}_FOUND)
        message(FATAL_ERROR "HEADER ${HEADER} NOT FOUND")
    endif()
endforeach()

```
