- CMake Cookbook
- Radovan Bast Roberto Di Remigio
- 314字
- 2025-04-04 16:17:18
How it works
find_package is a wrapper command for CMake modules written for discovering and setting up packages. These modules contain CMake commands to identify packages in standard locations on the system. The files for the CMake modules are called Find<name>.cmake and the commands they contain will be run internally when a call to find_package(<name>) is issued.
In addition to actually discovering the requested package on your system, find modules also set up a handful of useful variables, reflecting what was actually found, which you can use in your own CMakeLists.txt. In the case of the Python interpreter, the relevant module is FindPythonInterp.cmake, which is shipped with CMake, and sets the following variables:
- PYTHONINTERP_FOUND, a Boolean signaling whether the interpreter was found
- PYTHON_EXECUTABLE, the path to the executable for the Python interpreter
- PYTHON_VERSION_STRING, the full version of the Python interpreter
- PYTHON_VERSION_MAJOR, the major version of the Python interpreter
- PYTHON_VERSION_MINOR, the minor version of the Python interpreter
- PYTHON_VERSION_PATCH, the patch number of the Python interpreter
It is possible to force CMake to look for specific versions of a package. For example, use this to request any version of the Python interpreter greater or equal to 2.7:
find_package(PythonInterp 2.7)
It is also possible to enforce that dependencies are satisfied:
find_package(PythonInterp REQUIRED)
In this case, CMake will abort configuration if no suitable executable for the Python interpreter is found in the usual lookup locations.