How to Find Eigen3 with Cmake

Find package Eigen3 for CMake

Since Eigen3 is completely header only, all you ever need is the path to the include directory. And this one, you are already defining manually anyway. So there is no real need for a FindEigen3.cmake or FIND_PACKAGE call.

Simply use

INCLUDE_DIRECTORIES ( "$ENV{EIGEN3_INCLUDE_DIR}" )

or

SET( EIGEN3_INCLUDE_DIR "$ENV{EIGEN3_INCLUDE_DIR}" )
IF( NOT EIGEN3_INCLUDE_DIR )
MESSAGE( FATAL_ERROR "Please point the environment variable EIGEN3_INCLUDE_DIR to the include directory of your Eigen3 installation.")
ENDIF()
INCLUDE_DIRECTORIES ( "${EIGEN3_INCLUDE_DIR}" )

A few notes:

  1. If you want to access the content of a CMake variable, make sure to use ${...}
  2. $ENV{....} accesses environment variables.
  3. The second example will stop with an error if the environment variable is not set (and, thus, EIGEN3_INCLUDE_DIR cmake variable is empty)
  4. Be careful to use quotation marks around (evaluated) variables if they could contain whitespace. Otherwise, CMake will interpret it as a list.
  5. If you want to use custom find modules, make sure to either place them in you CMake installation or, as @Fraser pointed out above, make sure to point CMAKE_MODULE_PATH to the directory where it is. Not sure, but it could be that CMake checks the current directory as well automatically (where your CMakeLists.txt resides. Anyhow, setting EIGEN3_INCLUDE_DIR is totally unrelated to the location of FindEigen3.cmake
  6. However, it could be that your FindEigen3 script evaluates this variable to determine the location of your Eigen3 installation.
  7. Alternatively, self-built CMake-based projects often provide a <PackageName>Config.cmake. If you point a variable called <PackageName>_DIR to the directory containing this file, you can use FIND_PACKAGE( <PackageName> ...) as normal. See documentation of FIND_PACKAGE for details.

Unable to find Eigen3 with CMake

Turning my comment into an answer

The find package scripts - like FindEigen3.cmake - normally use the find_path() command to detect the package's include directory (see it's documentation for the full details).

FindEigen3.cmake uses the following code snippet:

find_path(EIGEN3_INCLUDE_DIR NAMES signature_of_eigen3_matrix_library
PATHS
${CMAKE_INSTALL_PREFIX}/include
${KDE4_INCLUDE_DIR}
PATH_SUFFIXES eigen3 eigen
)

So it looks in CMAKE_INSTALL_PREFIX which on Unix/Linux hosts is /usr/local by default.

The following has worked for me:

  • Go to the Eigen source directory and run the CMake and installation steps

    > mkdir build
    > cd build
    > cmake ..
    > make install
  • Then copy - as you have done - FindEigen3.cmake to your projects source directory.

  • Now your code does find Eigen (just changed to list(APPEND ...))

    list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}")
    find_package(Eigen3 REQUIRED)

References

  • Eigen & CMake
  • CMake: Of what use is find_package() if you need to specify CMAKE_MODULE_PATH anyway?
  • Cmake doesn't find Boost

Problem building C++ code with Eigen3 and CMake eigen3/Eigen/Core' file not found

The include directory defined in Eigen3::Eigen already includes eigen3 (e.g., /usr/include/eigen3 on Ubuntu). So you should use #include <Eigen/Core>.

You can check this by:

find_package(Eigen3 REQUIRED CONFIG)

# checking property of the target
get_target_property(inc_dir Eigen3::Eigen INTERFACE_INCLUDE_DIRECTORIES)
message("[DEBUG] inc_dir: ${inc_dir}")

# or checking the Eigen variable
message("[DEBUG] EIGEN3_INCLUDE_DIRS: ${EIGEN3_INCLUDE_DIRS}")

EIGEN3_INCLUDE_DIRS is defined in here.

Adding Eigen library to c++ project using cmake

The problems you are running into are caused by downloading the Eigen source code but not actually building the project. You may think since its a header only library that there is no build step. Well there is; it builds the package config .pc and Eigen3Config.cmake files. One of which you are trying to use.

From my previous comment the file Eigen3Config.cmake.in is a template and will be used to generate the Eigen3Config.cmake which then would be usable.

Its probably easier to install the libeigen3-dev package, it is packaged with /usr/lib/cmake/eigen3/Eigen3Config.cmake. If you insist on using Eigen from source then build and install it

If you DO want to download and install as source maybe take a look at the INSTALL file which has a "Method 2" suggesting how to use it in cmake.

CMake - Eigen3_DIR-NOTFOUND

To summarize the comments for completeness:

CMake's find_package() command has two modes of operation: Module and Config mode. This error essentially says Module mode failed, then Config mode failed to find the Eigen3 package:

  By not providing "FindEigen3.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "Eigen3", but
CMake did not find one.

Could not find a package configuration file provided by "Eigen3" with any
of the following names:

Eigen3Config.cmake
eigen3-config.cmake

Add the installation prefix of "Eigen3" to CMAKE_PREFIX_PATH or set
"Eigen3_DIR" to a directory containing one of the above files. If "Eigen3"
provides a separate development package or SDK, be sure it has been
installed.

In general, when a package XXX (such as Eigen3, for example) is installed, that package should configure the XXXConfig.cmake file. That way, external projects can find and use package XXX by calling find_package() in Config mode.

Because your Eigen3 package was not installed, the Eigen3Config.cmake file did not get configured. Thus, the Module mode search should work for you, as only the FindEigen3.cmake file exists in your Eigen3 directories. For Module mode, the path to the FindEigen3.cmake file must be added to the CMAKE_MODULE_PATH, as the error suggests. Adding this line before the find_package(Eigen3 ...) call allows CMake Module mode to succeed:

list(APPEND CMAKE_MODULE_PATH "C:/eigen-3.3.7/cmake")

cmake find_package unable to find Eigen3Config.cmake spectra Windows

There is no need to build/install Eigen to use it if you include it manually, as done in the getting started example (https://eigen.tuxfamily.org/dox/GettingStarted.html#title0)

But in order to be found by CMake, you will need to build / install it, as explained in the INSTALL file. https://gitlab.com/libeigen/eigen/-/blob/master/INSTALL

Usually, your error is followed by an hint asking you to set the variable Eigen3_DIR (or something similar) to point the build/install dir of the target project (Eigen3 here). It appears typically when you have built but not installed the project.

So:

  1. Build Eigen
  2. Install it (optional)
  3. For spectra set the cmake var Eigen3_DIR to /path/to/Eigen/build . (if eigen not install or still not found)


Related Topics



Leave a reply



Submit