Search code examples
c++cmakeeigenheader-only

Adding Eigen library to c++ project using cmake


Unfortunately, i have some problems adding the headers only Eigen 3.3.7 Library to my Makefile with Cmake on my Ubuntu 18.04.4 LTS system. I can compile my code using the library by just copying the library folder in the include directory and using include_directories(./include/eigen3) in the CMakeLists.txt file. However, i would much prefer not having the library path hard-coded but being able set it dynamically in the CMakeLists.txt file to make sharing the project with other people easier. Unfortunately, none of the instructions i found worked for me.

I prepared the following minimum code example:

main.cpp:

#include <eigen3/Eigen/Dense>
#include <iostream>

int main()
{  
    Eigen::Vector3d test_vec(1.0f, 2.0f, 3.0f);
    std::cout << test_vec << std::endl;
    return 0;
} 

CMakeLists.txt:

cmake_minimum_required(VERSION 3.0)

project(Eigen-Cmake-Test VERSION 1.0) # set the project name

find_package (Eigen3 3.3 REQUIRED NO_MODULE)

include_directories(${EIGEN_INCLUDE_DIR})

# add the executable
add_executable("${PROJECT_NAME}" ./main.cpp) 

target_link_libraries("${PROJECT_NAME}" Eigen3::Eigen)

I downloaded the headers only Eigen 3.3.7 Library and renamed the folder to eigen3. The folder was then moved to: /usr/local/share/eigen3

when i run cmake CMakeLists.txt i get the following error:

CMake Error at CMakeLists.txt:5 (find_package):
  Could not find a package configuration file provided by "Eigen3" (requested
  version 3.3) 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.


-- Configuring incomplete, errors occurred!

As i checked the Eigen library folder i realized that /usr/local/share/eigen3/cmake only contained a file named Eigen3Config.cmake.in instead of Eigen3Config.cmake which was required. Why is this the case?

I tried renaming the file to Eigen3Config.cmake. Here the error was the following:

CMake Error at CMakeLists.txt:5 (find_package):
  Could not find a configuration file for package "Eigen3" that is compatible
  with requested version "3.3".

  The following configuration files were considered but not accepted:

    /usr/local/share/eigen3/cmake/Eigen3Config.cmake, version: unknown



-- Configuring incomplete, errors occurred!

Furthermore, i also tried the solutions explained stackoverflow: CMake find_package not working for Eigen? without success.

How can i make this work properly? Also i think i do not quite understand the underlying system yet. Any explanation would be appreciated.


Solution

  • 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.