Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find package Eigen3 for CMake

Tags:

cmake

eigen

CMake cannot find my Eigen3 package. I set an environment variable called

EIGEN3_INCLUDE_DIR

pointing to the path where FindEigen3.cmake is.

Then in the CMakelists.txt I wrote:

find_package( Eigen3 REQUIRED )
include_directories( EIGEN3_INCLUDE_DIR )

I get next message of error:

CMake Error at C:/Program Files (x86)/CMake 2.8/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:91 (MESSAGE):
  Could NOT find Eigen3 (missing: EIGEN3_INCLUDE_DIR EIGEN3_VERSION_OK)
  (Required is at least version "2.91.0")
Call Stack (most recent call first):
  C:/Program Files (x86)/CMake 2.8/share/cmake-2.8/Modules/FindPackageHandleStandardArgs.cmake:252 (_FPHSA_FAILURE_MESSAGE)
  C:/Program Files (x86)/CMake 2.8/share/cmake-2.8/Modules/FindEigen3.cmake:76 (find_package_handle_standard_args)
  test/test_quaternion/CMakeLists.txt:25 (find_package)

Any idea on what I am missing or doing wrong?

like image 349
Jav_Rock Avatar asked Sep 03 '12 13:09

Jav_Rock


3 Answers

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.
like image 65
Johannes S. Avatar answered Nov 17 '22 00:11

Johannes S.


Eigen >= 3.3 has direct CMake integration, so it seems yours isn't set up correctly.

Assume Eigen has been installed to /opt/eigen/3.3 with default settings. Append or prepend the the location /opt/eigen/3.3 to the environment variable CMAKE_PREFIX_PATH e.g in bash:

export CMAKE_PREFIX_PATH="$CMAKE_PREFIX_PATH:/opt/eigen/3.3"

Then CMake should pick it up.


If you are writing your own CMakeLists.txt that uses Eigen I suggest using code like this:

find_package (Eigen3 3.3 REQUIRED)
add_executable (example example.cpp)
target_link_libraries (example Eigen3::Eigen)

You should not use include_directories since CMake 3 was released -- the targets approach should be preferred whenever available.

like image 39
Levi Morrison Avatar answered Nov 17 '22 02:11

Levi Morrison


First, make sure Eigen is properly installed. Refer to the INSTALL file that comes with the tarball.

Second, copy the cmake/FindEigen3.cmake file from the tarball to the directory containing your CMakeLists.txt.

In your CMakeLists.txt add:

set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR})
find_package(Eigen3 REQUIRED)
include_directories(${EIGEN3_INCLUDE_DIR})

Now you should be able to do e.g. #include <Eigen/Core>.

All of this comes (mostly) from this source.

This approach has the advantage over e.g. include_directories("$ENV{EIGEN3_INCLUDE_DIR}") that it the uses CMake's standard mechanism for finding external dependencies, making it easier for someone else (or your future self) to pick up the project, possibly on another platform.

(However, it would be nice if Eigen itself installed an EigenConfig.cmake file, making it accessible through the find_package mechanism without any extra paths.)

like image 11
Emil Avatar answered Nov 17 '22 01:11

Emil