How to do it

In this recipe, we set out to find the MPI implementation: library, header files, compiler wrappers, and launcher. To do so, we will leverage the FindMPI.cmake standard CMake module:

  1. First, we define the minimum CMake version, project name, supported language, and language standard:
cmake_minimum_required(VERSION 3.9 FATAL_ERROR)project(recipe-06 LANGUAGES CXX)set(CMAKE_CXX_STANDARD 11)set(CMAKE_CXX_EXTENSIONS OFF)set(CMAKE_CXX_STANDARD_REQUIRED ON)
  1. We then call find_package to locate the MPI implementation:
find_package(MPI REQUIRED)
  1. We define the executable name and, source, and similarly to the previous recipe, link against the imported target:
add_executable(hello-mpi hello-mpi.cpp)target_link_libraries(hello-mpi PUBLIC ...

Get CMake Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.