cmakelinkermpfr

Hot do I link MPFR in CMake?


I'm totally new to cmake, I'm on MacOs and I'm trying to build a c++ library and I need to link my executables to mpfr in order to make it work

This is my CMakeLists.txt file:

cmake_minimum_required(VERSION 3.19)
project(my_project)

set(CMAKE_CXX_STANDARD 14)
add_executable(my_project main.cpp)

find_package(GSL REQUIRED)
target_link_libraries(my_project GSL::gsl GSL::gslcblas)

find_package(Boost REQUIRED)
target_link_libraries(my_project Boost::boost)

find_package(MPFR REQUIRED) # <- It fails here!
target_link_libraries(my_project MPFR::mpfr)

When I try to build my project with CLion I get the following error:

CMake Error at CMakeLists.txt:13 (find_package):
  By not providing "FindMPFR.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "MPFR", but
  CMake did not find one.

  Could not find a package configuration file provided by "MPFR" with any of
  the following names:

    MPFRConfig.cmake
    mpfr-config.cmake

  Add the installation prefix of "MPFR" to CMAKE_PREFIX_PATH or set
  "MPFR_DIR" to a directory containing one of the above files.  If "MPFR"
  provides a separate development package or SDK, be sure it has been
  installed.

After some research I found out that Cmake was linking correctly both GSL and Boost because there are both a /usr/local/share/cmake/Modules/FindGSL.cmake and a /usr/local/share/cmake/Modules/FindBoost.cmake file, So I looked online for a FindMPFR.cmake file to insert into the /usr/local/share/cmake/Modules/ directory, I tried with this one but the error remains the same. What am I doing wrong?


Solution

  • Ok now my CMakeLists.txt file looks like this:

    cmake_minimum_required(VERSION 3.19)
    project(my_project)
    
    set(CMAKE_CXX_STANDARD 14)
    
    add_executable(my_project main.cpp )
    
    # Append the cmake/ directory to the CMAKE_MODULE_PATH
    list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
    
    find_package(GSL REQUIRED)
    message(STATUS "GSL Found: ${GSL_FOUND}")
    target_link_libraries(my_project GSL::gsl GSL::gslcblas)
    
    find_package(Boost REQUIRED)
    message(STATUS "Boost Found: ${Boost_FOUND}")
    target_link_libraries(my_project Boost::boost)
    
    
    find_package(MPFR REQUIRED)
    message(STATUS "MPFR Found: ${MPFR_FOUND}")
    target_link_libraries(my_project ${MPFR_LIBRARIES})
    

    And it works fine :)