Include mpi.h not found means your compiler can’t see MPI headers; install an MPI package and compile with its wrapper tools or include paths.
You hit Build, and the compiler stops at the first line that pulls in MPI. It’s frustrating, but the fix is usually plain: the headers exist somewhere on disk, yet your build is not looking there, or MPI is not installed at all.
This guide walks you through a clean set of checks and fixes for Linux, macOS, and Windows, plus the build-system tweaks that trip people up. You’ll end with a command you can run, a path you can verify, and a simple way to confirm the build is linked to the same MPI you plan to run.
What The mpi.h not found message means
The error is a preprocessor failure. When the compiler reads #include , it searches its include directories. If it can’t find that header, it stops. That’s all it is.
If you see include mpi.h not found in your build log, treat it as a search-path issue, not a code bug.
What makes it tricky is that MPI headers rarely live in a “standard” include folder. Many MPI installs tuck headers under a versioned directory, or they rely on wrapper compilers to inject the right -I flags automatically. On Windows, headers may sit under a vendor folder and only Visual Studio projects that point there will compile.
Three common root causes
- MPI isn’t installed — Your system has no MPI headers, so every compile fails the same way.
- You used the wrong compiler command — You called
gcc/g++directly instead ofmpicc/mpicxx, so the include flags never got added. - Your project points at the wrong MPI — You installed one MPI, then your build system is still configured for a different one, or a stale path.
Fast Checks That Save Time Before You Change Anything
Start with checks that don’t touch your system. You want to learn what’s already installed, where headers live, and which compiler wrapper you’re using. These steps keep your next fix targeted.
- Check for wrapper compilers — Run
mpicc --versionandmpicxx --version. If the command is missing, MPI is not on your PATH. - Ask the wrapper for flags — Run
mpicc -show(Open MPI) ormpicc -showme. You should see include flags like-I…. - Locate the header file — On Linux/macOS, try
locate mpi.horfind /usr -name mpi.h 2>/dev/null | head. You’re hunting for a real path you can paste into your build. - Confirm you’re building MPI code — A few libraries ship files named
mpi.hthat are not full MPI. The safe check is thatmpiccreports a vendor like Open MPI, MPICH, Intel MPI, or MS-MPI.
Quick decision table
| What you see | Likely cause | Next step |
|---|---|---|
mpicc not found |
MPI not installed or PATH not set | Install MPI, then reopen your terminal |
mpicc -show has no -I |
Broken install or wrong wrapper | Reinstall MPI or switch to a standard package |
mpi.h exists under a vendor folder |
Build lacks include path | Add that folder to include directories |
Fix Mpi.h Missing Errors For GCC And Clang Builds
On Unix-like systems, the cleanest fix is to stop calling the system compiler directly. MPI wrapper compilers are designed for this exact job. They supply the include paths, libraries, and link flags in the right order.
Use the wrapper compiler on the command line
- Compile C with mpicc — Run
mpicc -O2 hello.c -o helloinstead ofgcc. - Compile C++ with mpicxx — Run
mpicxx -O2 hello.cpp -o helloinstead ofg++. - Run with mpirun or mpiexec — Try
mpirun -n 2 ./helloso the runtime matches the compile-time MPI.
When you must call gcc or clang
Some build systems hard-code the compiler, or a project needs a specific toolchain. In that case, copy the flags from the wrapper into your build settings.
- Print the full compile line — Run
mpicc -show(or-showme) and copy the-Iand-Lparts into your build. - Add the include directory — Put the folder that contains
mpi.hinto your include search path, often via-I/path/to/include. - Link the matching MPI libs — If you add include paths by hand, also link the right libraries, or you’ll trade a header error for linker errors.
Linux Fixes For Open MPI, MPICH, And Intel MPI
Most Linux setups get MPI from a package manager, a module system on a cluster, or a vendor installer. The steps below aim at the most common cases and keep your build consistent.
Install Open MPI or MPICH from your distro
If you are on Ubuntu or Debian, you can install Open MPI with the packages that provide both runtime and development headers. Many distros split those parts, so grabbing only the runtime leaves out headers.
- Install Open MPI dev packages — Use your package manager to install Open MPI plus the development headers, then confirm
mpiccexists. - Install MPICH dev packages — If you prefer MPICH, install its development package and use its
mpiccwrapper. - Verify the header path — Run
mpicc -showand confirm it points at a folder that containsmpi.h.
Open MPI’s own quick-start page lists common install routes like distro packages and Homebrew/MacPorts for macOS. It also shows that wrapper compilers are the normal workflow. You can check the latest guidance in Open MPI’s docs. Open MPI installation quick start.
Handle versioned include directories
Some packages place headers under a versioned folder, not directly under /usr/include. That’s fine when you use mpicc. It fails when you call the base compiler without extra flags.
- Find the actual folder — Locate
mpi.hand copy the directory name, not the file path. - Add it once — Put that directory into your build include paths and rebuild.
- Remove stale paths — If you previously pointed at a different MPI, delete those old
-Ientries so you don’t mix headers.
macOS Fixes With Homebrew And MacPorts
macOS users often install Open MPI through Homebrew or MacPorts. Both routes give you wrapper compilers, and those wrappers are the simplest path out of the header error.
Install and confirm the wrapper compilers
- Install Open MPI — With Homebrew, run
brew install openmpi. With MacPorts, runsudo port install openmpi. - Confirm the tool is reachable — Run
mpicc --versionand make sure it prints vendor info. - Build with mpicc — Re-run your build, pointing the project at
mpicc/mpicxx.
Include MPI.H Not Found On Windows With Visual Studio Or VS Code
Windows builds usually fail because the project does not know where MS-MPI (or another MPI) is installed. The fix is to install a Windows MPI distribution, then point the compiler and linker at its include and lib folders.
Install MS-MPI and find its folders
Microsoft MPI installs under C:\\Program Files\\Microsoft MPI by default on many systems. You can download it from Microsoft’s site and install both the SDK and runtime. Microsoft MPI download.
- Install the MS-MPI SDK — The SDK includes headers like
mpi.hand import libraries. - Locate the include folder — Look for a path like
C:\\Program Files\\Microsoft MPI\\Inc. - Locate the lib folder — Look for
Lib\\x64orLib\\x86, matching your target.
Set Visual Studio project properties
- Add include directory — In C/C++ settings, add the MS-MPI
Incfolder to Additional Include Directories. - Add library directory — In Linker settings, add the matching MS-MPI
Libfolder to Additional Library Directories. - Link the import library — Add
msmpi.libto Additional Dependencies for the linker.
Make, CMake, And IDE Fixes That Prevent The Error From Returning
Once you can build from a terminal, lock it into your project so the error doesn’t reappear on the next machine or CI run. These fixes keep the build repeatable.
Makefiles and plain compiler variables
- Set CC and CXX to wrappers — Use
CC=mpiccandCXX=mpicxxso targets inherit MPI flags. - Keep flags minimal — Let wrappers decide the right include paths; add only your project’s own
-Ifolders. - Pin the runtime — Use
mpirunormpiexecfrom the same MPI install you compiled against.
CMake with find_package
CMake can locate MPI via its built-in module, then supply include dirs and libraries to your targets. That is usually cleaner than hard-coded paths.
- Enable MPI in CMake — Use
find_package(MPI REQUIRED)in your CMakeLists.txt. - Attach MPI to targets — Link your target with
MPI::MPI_CorMPI::MPI_CXXso include directories propagate. - Point CMake at the right wrappers — If discovery fails, set
CMAKE_C_COMPILERtompiccand rerun configuration.
When you share the project, include a note that names the MPI vendor and version you built with. Teammates can match it and avoid mismatched runs.
Multi-MPI systems and header mixing
It’s common to have Open MPI and MPICH installed side by side. Header mixing is where build pain starts: compile against one, run with another, and you get runtime crashes or weird hangs.
- Confirm which mpicc is active — Run
which mpiccand keep it stable during your build. - Check the runtime tool — Run
which mpirunorwhich mpiexecand ensure it is from the same tree. - Clear caches — Delete CMake cache files or your build directory after switching MPI vendors.
Verify The Fix And Avoid The Next Break
When the header error is gone, do one last round of checks so you know your program is wired to the MPI you expect. This takes two minutes and saves hours later.
Build and run a tiny test
- Compile a hello program — Build a minimal MPI hello world using the same command your project uses.
- Run with two ranks — Execute
mpirun -n 2 ./hello(ormpiexec -n 2 hello.exeon Windows) and confirm you see output from rank 0 and rank 1. - Print the MPI version — Call
MPI_Get_versionin your test and print it to confirm which MPI you are running.
Common traps that bring the error back
- IDE using a different compiler — Your terminal build may use
mpiccwhile your IDE uses plaingcc. Point the IDE at the wrapper, or copy flags frommpicc -show. - Mixing 32-bit and 64-bit libs — On Windows, include and lib folders must match the architecture you build.
If you still see the mpi.h not found error after these steps, treat it as a path problem: locate the real header, then make the compiler search that directory, either through the wrapper compilers or explicit include settings. Once that path is stable, the error goes away and stays away.
