TopicCMakeGuide.dox 1.9 KB

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