find_package(MPI) 和 FindMPI 有什么区别?

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

如 CMake 3.0 的 documentation 所述,find_package(MPI)FindMPI 似乎可以互换?但我的问题是:

我好像明白了。 find_package()FindMPI 是两个不同的东西。 find_package() 是一个 CMake 脚本命令,FindMPI 是一个 CMake 模块。

documentation of find_package() 中所述,可以 select 搜索包的 "Module" 模式。这意味着,当调用 find_package(MPI) 时,它将使用 FindMPI 模块(写在 FindMPI.cmake 文件中)来搜索 MPI 库。

当您尝试查找其他包时出现类似情况,所有包都已列出 here

因为 FindMPI is one of the Find Modules 由 CMake 安装提供,所以 find_package(MPI)include(FindMPI) 调用本质上是等价的。 (此处需要 include() 来加载模块;简单地在 CMake 文件中写入 FindMPI 将导致错误。)

find_package()命令有两种模式:MODULECONFIG。默认为 MODULE 模式,来自 find_package() 文档:

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.

因此,find_package(MPI) 将搜索名为FindMPI.cmake 的文件,这相当于命令include(FindMPI)。这成立,除非您在 CMAKE_MODULE_PATH 中定义了另一个 FindMPI.cmake 文件。

虽然它们本质上是等效的命令,但调用 find_package() 通常更有用,因为它允许您传递参数,例如 REQUIRED,以进一步指定如何加载外部项目的设置。