在旧的Macbook上,忽然发现无法开启MPI编译程序,运行 CMake 时会报如下错误:

-- Could NOT find MPI_C (missing: MPI_C_WORKS)
-- Could NOT find MPI_CXX (missing: MPI_CXX_WORKS)
CMake Error at /usr/local/Cellar/cmake/3.27.9/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
Could NOT find MPI (missing: MPI_C_FOUND MPI_CXX_FOUND)
Call Stack (most recent call first):
/usr/local/Cellar/cmake/3.27.9/share/cmake/Modules/FindPackageHandleStandardArgs.cmake:600 (_FPHSA_FAILURE_MESSAGE)
/usr/local/Cellar/cmake/3.27.9/share/cmake/Modules/FindMPI.cmake:1837 (find_package_handle_standard_args)
CMakeLists.txt:30 (find_package)

另一台Macboo安装了 OpenMPI 版本,可以正常编译MPI程序。这台笔记本安装的是 MPICH 版本,不知为何突然不行了。尝试过卸载重装 MPICH,新安装 OpenMPI 版本,问题依旧,看来是一些环境或者设置已经出问题了。

百般尝试无法解决的情况下,最终使用手动指定MPI库绕过了 CMake 的MPI检测:

cmake .. 其它选项 \
-DMPI_C_ADDITIONAL_INCLUDE_DIRS=/usr/local/Cellar/mpich/4.1.2/include \
-DMPI_C_LIBRARIES="-L/usr/local/Cellar/mpich/4.1.2/lib -lmpi -lpmpi" \
-DMPI_CXX_ADDITIONAL_INCLUDE_DIRS=/usr/local/Cellar/mpich/4.1.2/include \
-DMPI_CXX_LIBRARIES="-L/usr/local/Cellar/mpich/4.1.2/lib -lmpi -lpmpi" \
-DCMAKE_C_COMPILER=/usr/local/bin/mpicc \
-DCMAKE_CXX_COMPILER=/usr/local/bin/mpicxx

如果在 MacOS 或者 Linux 上遇到CMake无法找到MPI的问题OpenMPI,可以使用此方法解决。