fatal error: mpi.h: No such file or directory #include <mpi.h>

C++CMpiOpenmpi

C++ Problem Overview


when I compile my script with only

#include <mpi.h>

it tells me that there is no such file or directory. But when i include the path to mpi.h as

#include "/usr/include/mpi/mpi.h"

(the path is correct) it returns:

In file included from /usr/include/mpi/mpi.h:2087:0,
                 from lbm.cc:7:
/usr/include/mpi/openmpi/ompi/mpi/cxx/mpicxx.h:35:17: fatal error: mpi.h: No such file or directory
 #include "mpi.h"
                 ^
compilation terminated.

Anyone know how to fix this?

C++ Solutions


Solution 1 - C++

The problem is almost certainly that you're not using the MPI compiler wrappers. Whenever you're compiling an MPI program, you should use the MPI wrappers:

  • C - mpicc
  • C++ - mpiCC, mpicxx, mpic++
  • FORTRAN - mpifort, mpif77, mpif90

These wrappers do all of the dirty work for you of making sure that all of the appropriate compiler flags, libraries, include directories, library directories, etc. are included when you compile your program.

Solution 2 - C++

On my system, I was just missing the Linux package.

sudo apt install libopenmpi-dev
pip install mpi4py

(example of something that uses it that is a good instant test to see if it succeeded)

Succeded.

Solution 3 - C++

You can execute:

$ mpicc -showme 

result :

gcc -I/Users/<USER_NAME>/openmpi-2.0.1/include -L/Users/<USER_NAME>/openmpi-2.0.1/lib -lmp

This command shows you the necessary libraries to compile mpicc

Example:

$ mpicc -g  -I/Users/<USER_NAME>/openmpi-2.0.1/include -o [nameExec] [objetcs.o...] [program.c] -lm


$ mpicc -g  -I/Users/<USER_NAME>/openmpi-2.0.1/include -o example file_object.o my_program.c otherlib.o -lm

this command generates executable with your program in example, you can execute :

$ ./example

Solution 4 - C++

On my system Ubuntu 16.04. I installed :

sudo apt install libopenmpi-dev

after I used mpiCC to compile and it works

Solution 5 - C++

As suggested above the inclusion of

/usr/lib/openmpi/include 

in the include path takes care of this (in my case)

Solution 6 - C++

Debian appears to include the following:

  • mpiCC.openmpi
  • mpic++.openmpi
  • mpicc.openmpi
  • mpicxx.openmpi
  • mpif77.openmpi
  • mpif90.openmpi

I'll test symlinks of each for mpic, etc., and see if that helps the likes of HDF5-openmpi enabled find mpi.h.

Take that back Debian includes symlinks via their alternatives system and it still cannot find the proper paths between HDF5 openmpi packages and mpi.h referenced in the H5public.h header.

Solution 7 - C++

On Ubuntu 18.04 I had to install:

sudo apt install lam4-dev

Solution 8 - C++

On Fedora:

dnf install openmpi-devel

Solution 9 - C++

On Mac 12.2, I installed with brew install openmpi. The header file is under /opt/homebrew/Cellar/open-mpi/x.x.x/include.

Solution 10 - C++

once you have mpi installed:

> $ sudo apt install mpich

see where the library is installed, each case is different:

> $ mpicc -show

in my case: (Ubuntu 20.0)

and add...

#include </usr/lib/x86_64-linux-gnu/openmpi/include/openmpi>

:-)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser2804865View Question on Stackoverflow
Solution 1 - C++Wesley BlandView Answer on Stackoverflow
Solution 2 - C++Gregory Alan BolcerView Answer on Stackoverflow
Solution 3 - C++BvacavarView Answer on Stackoverflow
Solution 4 - C++user7484492View Answer on Stackoverflow
Solution 5 - C++jeremy_rutmanView Answer on Stackoverflow
Solution 6 - C++Marc J. DriftmeyerView Answer on Stackoverflow
Solution 7 - C++Felix CrazzolaraView Answer on Stackoverflow
Solution 8 - C++Hung TranView Answer on Stackoverflow
Solution 9 - C++yyFredView Answer on Stackoverflow
Solution 10 - C++Carlos NoéView Answer on Stackoverflow