Eigen provides native CMake support which allows the library to be easily used in CMake projects.
Eigen exports a CMake target called Eigen3::Eigen
which can be imported using the find_package
CMake command and used by calling target_link_libraries
as in the following example:
The above code snippet must be placed in a file called CMakeLists.txt
alongside example.cpp
. After running
CMake will produce project files that generate an executable called example
which requires at least version 3.3 of Eigen. Here, path-to-example-directory
is the path to the directory that contains both CMakeLists.txt
and example.cpp
.
Do not forget to set the CMAKE_PREFIX_PATH
variable if Eigen is not installed in a default location or if you want to pick a specific version. For instance:
An alternative is to set the Eigen3_DIR
cmake's variable to the respective path containing the Eigen3*
.cmake files. For instance:
If the REQUIRED
option is omitted when locating Eigen using find_package
, one can check whether the package was found as follows: