blob: cf767d0ddabacd1a5abdfc7b3ad742defde1c66c [file] [log] [blame]
Austin Schuh189376f2018-12-20 22:11:15 +11001namespace Eigen {
2
3/**
4
5\page TopicCMakeGuide Using %Eigen in CMake Projects
6
7%Eigen provides native CMake support which allows the library to be easily
8used in CMake projects.
9
10\note %CMake 3.0 (or later) is required to enable this functionality.
11
12%Eigen exports a CMake target called `Eigen3::Eigen` which can be imported
13using the `find_package` CMake command and used by calling
14`target_link_libraries` as in the following example:
15\code{.cmake}
16cmake_minimum_required (VERSION 3.0)
17project (myproject)
18
19find_package (Eigen3 3.3 REQUIRED NO_MODULE)
20
21add_executable (example example.cpp)
22target_link_libraries (example Eigen3::Eigen)
23\endcode
24
25The above code snippet must be placed in a file called `CMakeLists.txt` alongside
26`example.cpp`. After running
27\code{.sh}
28$ cmake path-to-example-directory
29\endcode
30CMake will produce project files that generate an executable called `example`
31which requires at least version 3.3 of %Eigen. Here, `path-to-example-directory`
32is the path to the directory that contains both `CMakeLists.txt` and
33`example.cpp`.
34
Austin Schuhc55b0172022-02-20 17:52:35 -080035Do not forget to set the <a href="https://cmake.org/cmake/help/v3.7/variable/CMAKE_PREFIX_PATH.html">\c CMAKE_PREFIX_PATH </a> variable if Eigen is not installed in a default location or if you want to pick a specific version. For instance:
36\code{.sh}
37$ cmake path-to-example-directory -DCMAKE_PREFIX_PATH=$HOME/mypackages
38\endcode
39An alternative is to set the \c Eigen3_DIR cmake's variable to the respective path containing the \c Eigen3*.cmake files. For instance:
40\code{.sh}
41$ cmake path-to-example-directory -DEigen3_DIR=$HOME/mypackages/share/eigen3/cmake/
Austin Schuh189376f2018-12-20 22:11:15 +110042\endcode
43
44If the `REQUIRED` option is omitted when locating %Eigen using
45`find_package`, one can check whether the package was found as follows:
46\code{.cmake}
47find_package (Eigen3 3.3 NO_MODULE)
48
49if (TARGET Eigen3::Eigen)
50 # Use the imported target
51endif (TARGET Eigen3::Eigen)
52\endcode
53
54*/
55
56}