Song Wang
Song Wang

Reputation: 318

What's the difference between find_package(MPI) and FindMPI?

As stated in the documentation for CMake 3.0, find_package(MPI) and FindMPI seem to be interchangeable? But my questions are:

Upvotes: 0

Views: 680

Answers (2)

Song Wang
Song Wang

Reputation: 318

I seem to figure it out. find_package() and FindMPI are two different things. While find_package() is a CMake scripting command, FindMPI is a CMake Module.

As stated in the documentation of find_package(), one can select the "Module" mode by which it searches for packages. That means, when one calls find_package(MPI), it will make use of the FindMPI module (written in the FindMPI.cmake file) to search for the MPI library.

Similar things when you try to find other packages, all of which are listed here.

Upvotes: 1

Kevin
Kevin

Reputation: 18313

Because FindMPI is one of the Find Modules provided by the CMake installation, the find_package(MPI) and include(FindMPI) calls are essentially equivalent. (The include() is required here to load the module; simply writing FindMPI in a CMake file will result in an error.)

The find_package() command has two modes: MODULE and CONFIG. The default is MODULE mode, and from the find_package() documentation:

In Module mode, CMake searches for a file called Find<PackageName>.cmake. The file is first searched in the CMAKE_MODULE_PATH, then among the Find Modules provided by the CMake installation.

Therefore, find_package(MPI) will search for a file called FindMPI.cmake, which is equivalent to the command include(FindMPI). This holds up, unless you have another FindMPI.cmake file defined in your CMAKE_MODULE_PATH.

While they essentially equivalent commands, calling find_package() is typically more useful, as it lets you pass arguments, such as REQUIRED, to further specify how the settings of the external project are loaded.

Upvotes: 4

Related Questions