针对特定目标绕过 CMake 中的“find_package”?

Bypass `find_package` in CMake for specific targets?

有没有办法让一个特定的目标在 find_package 失败的情况下仍然能够构建?例如,我有一个只编译代码文档的目标,自然没有硬 requirements/dependencies,但 cmake 甚至不会完成配置,以防缺少依赖项。

您是否阅读了这部分文档:

find_package — CMake 3.23.0-rc2 Documentation

Regardless of the mode used, a <PackageName>_FOUND variable will be set to indicate whether the package was found. When the package is found, package-specific information may be provided through other variables and Imported Targets documented by the package itself. The QUIET option disables informational messages, including those indicating that the package cannot be found if it is not REQUIRED. The REQUIRED option stops processing with an error message if the package cannot be found.

A package-specific list of required components may be listed after the COMPONENTS keyword. If any of these components are not able to be satisfied, the package overall is considered to be not found. If the REQUIRED option is also present, this is treated as a fatal error, otherwise execution still continues. As a form of shorthand, if the REQUIRED option is present, the COMPONENTS keyword can be omitted and the required components can be listed directly after REQUIRED.

Additional optional components may be listed after OPTIONAL_COMPONENTS. If these cannot be satisfied, the package overall can still be considered found, as long as all required components are satisfied.

备注关键词:QUIETREQUIREDOPTIONAL_COMPONENTS

您可以通过使用 <PackageName>_FOUND 来处理失败来改变您的 cmake 代码的行为。