C++ boost/multiprecision: fatal error: mpfr.h: No such file or directory

3.2k views Asked by At

I have followed this answer and used Boost.Multiprecision to use high accuracy floating point numbers (examples).

main.cpp

#include <iostream>
#include <boost/multiprecision/mpfr.hpp>  // Defines the Backend type that wraps MPFR

int main(int argc, char** argv)
{
    namespace mp = boost::multiprecision;     // Reduce the typing a bit later...
    typedef mp::number<mp::mpfr_float_backend<300> >  my_float;
    my_float a, b, c; // These variables have 300 decimal digits precision

    return 0;
}

However, I have a serious problem for compilation of this code as I receive the following error:

/usr/include/boost/multiprecision/mpfr.hpp:15:18:
              fatal error: mpfr.h: No such file or directory

Even installing libgmp3-dev and gmplib did not work.

What is wrong?

CMakeLists.txt

cmake_minimum_required(VERSION 2.8.9)
project (main)

# Libraries
set(Boost_USE_STATIC_LIBS OFF) 
set(Boost_USE_MULTITHREADED ON)  
set(Boost_USE_STATIC_RUNTIME OFF) 
find_package(Boost 1.54.0 COMPONENTS filesystem regex system thread date_time wave) 

if(NOT Boost_FOUND)
    message( FATAL_ERROR "Boost 1.54.0 not found." )
endif()
include_directories(SYSTEM ${Boost_INCLUDE_DIR})

# Flags
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wfatal-errors -std=c++11")


include_directories(${Boost_INCLUDE_DIRS}) 

# pre executable commands


add_executable(main
    main.cpp
)


# Link
target_link_libraries(main ${Boost_LIBRARIES})
target_link_libraries(main ${CMAKE_THREAD_LIBS_INIT})
2

There are 2 answers

0
davidbak On BEST ANSWER

If you want to use the MPFR backend you've got to install it separately, make sure it is built, and make sure its headers are in your compiler's INCLUDE path and its binaries (libraries) are in your linker's command line.

(MPFR is not GMP.)

0
user83395 On

You must include the libraries for both gmp and mfr in your cmake file.

In qmake use: LIBS += -lgmp -lmpfr

If you do not know how in cmake, just let qmake make a cmake file, I do not use cmake, but I know it is similar, but forgot how to do it, or I would just do it and post the results, sorry, but of all the things I have forgotten over the years, like where did I put my Memories; but like them, this post is Old, so anyone finding it might find this helpful.

This will satisfy the includes below, and a few more:

 #include <boost/multiprecision/cpp_dec_float.hpp>
 #include <boost/multiprecision/mpfr.hpp>
 #include <boost/multiprecision/gmp.hpp>