CMake: set path to MPI headers and binaries manually

Viewed 8695

I am developing an MPI application which requires to be run with a specific implementation of MPI (let's call it MPIvA). On my workstation, another implementation of MPI (let's call it MPIvB) is installed.

My application is built using CMake and the find_library(MPI) obviously points to MPIvB. It compiles and runs without hassle.

I compiled MPIvA on my workstation. How can I make CMake use these headers and binaries?

5 Answers

In this specific case, I succeeded using the following environment variable:

export MPI_HOME=/your/path/to/prefix

Please set it before using cmake, or empty the build directory.

Well, this is not a new post but it may be useful to others in the future.

Even though the previous answers can also work, I think that the a better approach is to use the CMAKE_PREFIX_PATH option, ie sth like

CMAKE_PREFIX_PATH=~/my-libs-install-path ccmake ..

Now, some other comments:

  1. All MPI libraries should be interchangeable, ie code that uses the MPI specification should compile and run with any MPI implementation (eg MPIvA and MPIvB which I am assuming are intelmpi,openmpi or mpich).

  2. Regarding this answer (my reputation doesn't allow me to comment so I reply here):

find_package(MPI REQUIRED)
if (MPI_FOUND)
    include_directories(SYSTEM ${MPI_INCLUDE_PATH})
else (MPI_FOUND)
    message(SEND_ERROR "This application cannot compile without MPI")
endif (MPI_FOUND)

even though the functionality would be mostly as expected, the following is functionally equivalent:

find_package(MPI REQUIRED)
include_directories(SYSTEM ${MPI_INCLUDE_PATH})

The REQUIRED part in the findpackage(MPI REQUIRED) command will cause the command to fail if MPI is not found, so the if(MPI_FOUND) part is useless and the else clause will actually never be executed.

  1. Finally, regarding the include_directories command, in my tests was not required if you are using targets, eg

    target_link_libraries(my_app PUBLIC MPI::C)

is enough.

Related