- CMake Cookbook
- Radovan Bast Roberto Di Remigio
- 241字
- 2025-04-04 16:17:18
How it works
Find-modules typically follow a specific pattern:
- Check whether the user provided a custom location for the desired package.
- Use commands from the find_ family to search for known required components of the required package, that is, header files, libraries, executables, and so forth. We have used find_path to find the full path to a header file and find_library to find a library. CMake also offers find_file, find_program, and find_package. These commands have the following general signature:
find_path(<VAR> NAMES name PATHS paths)
- Here, <VAR> will hold the result of the search, if successful, or <VAR>-NOTFOUND if unsuccessful. NAMES and PATHS are names for the file CMake should look for and paths where the search should be directed, respectively.
- From the results of this preliminary search, a version number is extracted. In our example, the ZeroMQ header file contains the library version, which can be extracted with string operations and regular expressions.
- Finally, the find_package_handle_standard_args command is invoked. This will handle the standard REQUIRED, QUIET, and version arguments to the find_package command, additionally setting the ZeroMQ_FOUND variable.
The full documentation for any CMake command can be obtained from the command line. For example, cmake --help-command find_file will output the manual page for the find_file command. For the manual page of CMake standard modules, use the --help-module CLI switch. For example, cmake --help-module FindPackageHandleStandardArgs will output to screen the manual page for the FindPackageHandleStandardArgs.cmake module.