Search code examples
c++cmakeboost-multiprecision

Use Quadmath and boost::multiprecision with CMake


I started writing a CMake based project, and I have installed the boost library and Quadmath, using Arch Linux and GCC.

When I use:

#include <boost/multiprecision/float128.hpp>

It raises the error that 'quadmath.h' is not found. But it is definetly present in my system under /usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/include.

I tried to write my own FindQuadmath.cmake with this content:

include(FindPackageHandleStandardArgs)

include_directories("/usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/include")

find_path(Quadmath_INCLUDE_DIR NAMES quadmath.h
    PATHS "/usr/lib/gcc/x86_64-pc-linux-gnu/14.2.1/include")
find_library(Quadmath_LIBRARY NAMES quadmath)

message("Quadmath_INCLUDE_DIR: ${Quadmath_INCLUDE_DIR}")
message("Quadmath_LIBRARY: ${Quadmath_LIBRARY}")

add_library(Quadmath SHARED IMPORTED ${Quadmath_LIBRARY})
target_include_directories(Quadmath INTERFACE ${Quadmath_INCLUDE_DIR})

set_property(TARGET Quadmath PROPERTY IMPORTED_LOCATION ${Quadmath_LIBRARY})
set_property(TARGET Quadmath PROPERTY INCLUDE_DIRECTORIES ${Quadmath_INCLUDE_DIR})

find_package_handle_standard_args(Quadmath DEFAULT_MSG Quadmath_LIBRARY Quadmath_INCLUDE_DIR)

In the top level Cmake, I include this FindQuadmath.

Current project structure:

├── cmake
│   └── FindQuadmath.cmake
├── CMakeLists.txt
├── conanfile.txt
├── conan_profiles
│   └── phd
├── README.md
├── requirements.txt
├── solvers
│   ├── CMakeLists.txt
│   └── heat_transfer_explicit_scheme
│       ├── CMakeLists.txt
│       ├── include
│       │   └── heat_transfer_explicit_scheme.hpp
│       └── src
│           └── heat_transfer_explicit_scheme.cpp
└── tests
cmake_minimum_required(VERSION 3.30)

project(phd_project CXX)

include(cmake/FindQuadmath.cmake)

find_package(Boost REQUIRED)

add_subdirectory(solvers/)

Neither I, nor boost can find headers such as <quadmath.h> and others from this lib.

How do I fix this and link Quadmath with CMake?


Solution

  • I have multiple issues.

    1. usage of some boost::multiprecision::float128 requires adding explicit link option:
    add_link_options(
        -lquadmath)
    

    For some reason, Boost CMake target does not encapsulate it? After that, the code can be compiled and executed, thus, it is a solution for the issue from this thread. This note I found here: https://www.boost.org/doc/libs/1_65_0/libs/math/example/float128_example.cpp

    1. in Qt Creator, I still have a lexical/preprocessor issue to line with code using <quadmath.h>, but I posted the question about that here: clangd false positive '<quadmath.h>' not found