- CMake Cookbook
- Radovan Bast Roberto Di Remigio
- 191字
- 2025-04-04 16:17:18
How it works
Eigen provides native CMake support, which makes it easy to set up a C++ project using it. Starting from version 3.3, Eigen ships CMake modules that export the appropriate target, Eigen3::Eigen, which we have used here.
You will have noticed the CONFIG option to the find_package command. This signals to CMake that the package search will not proceed through a FindEigen3.cmake module, but rather through the Eigen3Config.cmake, Eigen3ConfigVersion.cmake, and Eigen3Targets.cmake files provided by the Eigen3 package in the standard location, <installation-prefix>/share/eigen3/cmake. This package location mode is called "Config" mode and is more versatile than the Find<package>.cmake approach we have been using so far. For more information about "Module" mode versus "Config" mode, please consult the official documentation at https://cmake.org/cmake/help/v3.5/command/find_package.html.
Also note that while the Eigen3, BLAS, and OpenMP dependencies were declared as PUBLIC dependencies, the EIGEN_USE_BLAS compile definition was declared as PRIVATE. Instead of linking the executable directly, we could collect the library dependencies in a separate library target. Using the PUBLIC/PRIVATE keywords, we can then adjust the visibility of the corresponding flags and definitions to dependents of the library target.