1*bf2c3715SXin Linamespace Eigen { 2*bf2c3715SXin Li 3*bf2c3715SXin Li/** 4*bf2c3715SXin Li 5*bf2c3715SXin Li\page TopicCMakeGuide Using %Eigen in CMake Projects 6*bf2c3715SXin Li 7*bf2c3715SXin Li%Eigen provides native CMake support which allows the library to be easily 8*bf2c3715SXin Liused in CMake projects. 9*bf2c3715SXin Li 10*bf2c3715SXin Li\note %CMake 3.0 (or later) is required to enable this functionality. 11*bf2c3715SXin Li 12*bf2c3715SXin Li%Eigen exports a CMake target called `Eigen3::Eigen` which can be imported 13*bf2c3715SXin Liusing the `find_package` CMake command and used by calling 14*bf2c3715SXin Li`target_link_libraries` as in the following example: 15*bf2c3715SXin Li\code{.cmake} 16*bf2c3715SXin Licmake_minimum_required (VERSION 3.0) 17*bf2c3715SXin Liproject (myproject) 18*bf2c3715SXin Li 19*bf2c3715SXin Lifind_package (Eigen3 3.3 REQUIRED NO_MODULE) 20*bf2c3715SXin Li 21*bf2c3715SXin Liadd_executable (example example.cpp) 22*bf2c3715SXin Litarget_link_libraries (example Eigen3::Eigen) 23*bf2c3715SXin Li\endcode 24*bf2c3715SXin Li 25*bf2c3715SXin LiThe above code snippet must be placed in a file called `CMakeLists.txt` alongside 26*bf2c3715SXin Li`example.cpp`. After running 27*bf2c3715SXin Li\code{.sh} 28*bf2c3715SXin Li$ cmake path-to-example-directory 29*bf2c3715SXin Li\endcode 30*bf2c3715SXin LiCMake will produce project files that generate an executable called `example` 31*bf2c3715SXin Liwhich requires at least version 3.3 of %Eigen. Here, `path-to-example-directory` 32*bf2c3715SXin Liis the path to the directory that contains both `CMakeLists.txt` and 33*bf2c3715SXin Li`example.cpp`. 34*bf2c3715SXin Li 35*bf2c3715SXin LiDo 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*bf2c3715SXin Li\code{.sh} 37*bf2c3715SXin Li$ cmake path-to-example-directory -DCMAKE_PREFIX_PATH=$HOME/mypackages 38*bf2c3715SXin Li\endcode 39*bf2c3715SXin LiAn alternative is to set the \c Eigen3_DIR cmake's variable to the respective path containing the \c Eigen3*.cmake files. For instance: 40*bf2c3715SXin Li\code{.sh} 41*bf2c3715SXin Li$ cmake path-to-example-directory -DEigen3_DIR=$HOME/mypackages/share/eigen3/cmake/ 42*bf2c3715SXin Li\endcode 43*bf2c3715SXin Li 44*bf2c3715SXin LiIf the `REQUIRED` option is omitted when locating %Eigen using 45*bf2c3715SXin Li`find_package`, one can check whether the package was found as follows: 46*bf2c3715SXin Li\code{.cmake} 47*bf2c3715SXin Lifind_package (Eigen3 3.3 NO_MODULE) 48*bf2c3715SXin Li 49*bf2c3715SXin Liif (TARGET Eigen3::Eigen) 50*bf2c3715SXin Li # Use the imported target 51*bf2c3715SXin Liendif (TARGET Eigen3::Eigen) 52*bf2c3715SXin Li\endcode 53*bf2c3715SXin Li 54*bf2c3715SXin Li*/ 55*bf2c3715SXin Li 56*bf2c3715SXin Li} 57