1
votes

CMake does not find MPI for Fortran.

I am on Arch Linux with CMake 3.19.2. I installed the openmpi (4.0.5-2) package and as stated here I also installed the gcc-fortran (10.2.0-4) package for fortran support.

I my CMake script in the line where MPI should be found:

find_package(MPI REQUIRED)

it tells me that it could not find MPI for Fortran:

-- Found MPI_C: /usr/lib/openmpi/libmpi.so (found version "3.1")                                        
-- Found MPI_CXX: /usr/lib/openmpi/libmpi_cxx.so (found version "3.1")                                  
-- Could NOT find MPI_Fortran (missing: MPI_Fortran_WORKS) 
CMake Error at /usr/share/cmake-3.19/Modules/FindPackageHandleStandardArgs.cmake:218 (message):
  Could NOT find MPI (missing: MPI_Fortran_FOUND) (found version "3.1")
Call Stack (most recent call first):
  /usr/share/cmake-3.19/Modules/FindPackageHandleStandardArgs.cmake:582 (_FPHSA_FAILURE_MESSAGE)
  /usr/share/cmake-3.19/Modules/FindMPI.cmake:1721 (find_package_handle_standard_args)
  CMakeLists.txt:387 (find_package)

When installing openmpi it tells me that fortran support should be enabled:

(2/2) installing openmpi

Optional dependencies for openmpi
    gcc-fortran: fortran support [installed]

There is a file /usr/lib/openmpi/libmpi_mpifh.so. Correct me if I am wrong but I think this should be the correct library file.

Thanks in advance for your help!

EDIT

I just tried to build a minimal example:

project(test)
cmake_minimum_required(VERSION 3.0)
enable_language(Fortran)           
find_package(MPI REQUIRED)

With this it finds MPI_Fortran

-- Found MPI_Fortran: /usr/lib/openmpi/libmpi_usempif08.so (found version "3.1")

The CMakeLists.txt is a rather long and legacy one... Seems that I have to find the solution on my own.

2
It works for me on Arch Linux with the same package versions as yours. Check that /usr/lib/openmpi/mpi.mod exists and that you are not setting some strange value to FC.Hristo Iliev

2 Answers

1
votes

I solved the problem. As said in the edit of the answer this is legacy cmake and not written by myself.

My fortran skills are not too advanced so I do not know why this is used but the following line did break the finding process of the library:

set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -heap-arrays 0")

If anyone falls into the same trap try to avoid this!

0
votes

You need to use the COMPONENTS keyword on the find_package command, e.g.

find_package(MPI REQUIRED COMPONENTS Fortran)

Be sure you have Fortran enabled in your CMakeLists.txt either by enabling it in the project command, i.e.

project(name LANGUAGES Fortran)

or by calling

enable_language(Fortran)

prior to find_package.