- CMake Cookbook
- Radovan Bast Roberto Di Remigio
- 231字
- 2025-04-04 16:17:18
How it works
Remember that the compiler wrapper is a thin layer around the compiler used to build the MPI library. Under the hood, it will call the same compiler and augment it with additional arguments, such as include paths and libraries, needed to successfully build a parallel program.
Which flags does the wrapper actually apply when compiling and linking a source file? We can probe this using the --showme option to the compiler wrapper. To find out the compiler flags we can use:
$ mpicxx --showme:compile
-pthread
Whereas to find out the linker flags we use the following:
$ mpicxx --showme:link
-pthread -Wl,-rpath -Wl,/usr/lib/openmpi -Wl,--enable-new-dtags -L/usr/lib/openmpi -lmpi_cxx -lmpi
Similarly to the previous OpenMP recipe, we have found the linking to MPI to be extremely compact thanks to the imported targets provided by a reasonably modern FindMPI module:
target_link_libraries(hello-mpi
PUBLIC
MPI::MPI_CXX
)
We did not have to worry about compile flags or about include directories - these settings and dependencies are already encoded as INTERFACE-type properties in the IMPORTED target provided by CMake.
And as discussed in the previous recipe, for CMake versions below 3.9, we would have to do a bit more work:
add_executable(hello-mpi hello-mpi.c)
target_compile_options(hello-mpi
PUBLIC
${MPI_CXX_COMPILE_FLAGS}
)
target_include_directories(hello-mpi
PUBLIC
${MPI_CXX_INCLUDE_PATH}
)
target_link_libraries(hello-mpi
PUBLIC
${MPI_CXX_LIBRARIES}
)
In this recipe, we have discussed C++, but the arguments and approach are equally valid for a C or Fortran project.