Search code examples
c++cmakebuildmpi

How should I build MPI C++ programs with CMake?


I've not used MPI before - and frankly, I'm not really using it now, but I need to convert a Makefile which builds an MPI program into a CMakeLists.txt for a subdirectory of a project of mine. I'm wondering how to do this, given that it's apparently customary to invoke the compiler different for MPI C++ programs (mpic++).

How should I build an MPI program using CMake?


Solution

  • It seems it's not that complicated after all:

    find_package(MPI REQUIRED)
    add_executable(foo foo.cpp)
    target_link_libraries(foo MPI::MPI_CXX)
    

    But you might still need to use mpi_run to run your program.

    I'm still not sure about the use of mpic++. The accepted answer to this question claims it's just a wrapper script, but - it is not a wrapper script on my system.