Using Eigen in CMake Projects

Eigen provides native CMake support which allows the library to be easily used in CMake projects.

Note
CMake 3.0 (or later) is required to enable this functionality.

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:

1 cmake_minimum_required (VERSION 3.0)
2 project (myproject)
3 
4 find_package (Eigen3 3.3 REQUIRED NO_MODULE)
5 
6 add_executable (example example.cpp)
7 target_link_libraries (example Eigen3::Eigen)

The above code snippet must be placed in a file called CMakeLists.txt alongside example.cpp. After running

1 $ cmake path-to-example-directory

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.

If you have multiple installed version of Eigen, you can pick your favorite one by setting the Eigen3_DIR cmake's variable to the respective path containing the Eigen3*.cmake files. For instance:

cmake path-to-example-directory -DEigen3_DIR=$HOME/mypackages/share/eigen3/cmake/

If the REQUIRED option is omitted when locating Eigen using find_package, one can check whether the package was found as follows:

1 find_package (Eigen3 3.3 NO_MODULE)
2 
3 if (TARGET Eigen3::Eigen)
4  # Use the imported target
5 endif (TARGET Eigen3::Eigen)


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:51:44