Search code examples
c++cmakeeigen

CMake find eigen incorrect results


I am trying to compile a code snippet with Eigen using CMake, but CMake seems to have trouble finding the correct path, even if the source code is there.

This is my CMakeLists.txt file.

cmake_minimum_required(VERSION 3.14.2)
project(test)

# set default build type
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Release)
endif()


find_package(OpenCV 3 REQUIRED)
find_package (Eigen3 3.3 REQUIRED NO_MODULE)

include_directories(
        include
        ${OpenCV_INCLUDE_DIRS}
        ${EIGEN3_INCLUDE_DIR}
)


add_executable(playground playground.cpp)
target_link_libraries(playground ${OpenCV_LIBS} Eigen3::Eigen)

This is how I import headers in playground.cpp:

#include <iostream>
#include <vector>
#include <map>
#include <math.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <string>
#include <Eigen>

My Eigen is installed under /usr/include/eigen3, here is the directory structure:

yuqiong@yuqiong-G7-7588:/usr/include/eigen3$ ls
Eigen  signature_of_eigen3_matrix_library  unsupported

So the correct path to Eigen library is /usr/include/eigen3/Eigen.

However, when I run cmake .. and then make for the aforementioned CMakeLists.txt file, with the -LH flag, CMake complains it can't find Eigen. This is the path it thinks where Eigen is:

yuqiong@yuqiong-G7-7588:/media/yuqiong/DATA/vignetting/catkin_ws/src/vig2/src/build$ cmake -LH ..
-- Configuring done
-- Generating done
-- Build files have been written to: /media/yuqiong/DATA/vignetting/catkin_ws/src/vig2/src/build
-- Cache values
// Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel ...
CMAKE_BUILD_TYPE:STRING=

// Install path prefix, prepended onto install directories.
CMAKE_INSTALL_PREFIX:PATH=/usr/local

// The directory containing a CMake configuration file for Eigen3.
Eigen3_DIR:PATH=/usr/local/share/eigen3/cmake

// The directory containing a CMake configuration file for OpenCV.
OpenCV_DIR:PATH=/opt/ros/kinetic/share/OpenCV-3.3.1-dev

This is the structure of the folder where it think Eigen is:

yuqiong@yuqiong-G7-7588:/usr/local/share/eigen3$ tree
.
└── cmake
    ├── Eigen3Config.cmake
    ├── Eigen3ConfigVersion.cmake
    ├── Eigen3Targets.cmake
    └── UseEigen3.cmake

I am confused why CMake has such behavior? How can I let it find the correct path of Eigen in this case?

All this environment set-ups are so tricky to get it right...


Solution

  • Thanks for the comments above. I think the issue might be the Eigen team changed their directory structure somehow between different versions, and the old include path does not work anymore.

    The solution for me was to change #include <Eigen/Dense> to #include <eigen3/Eigen/Dense>. After that g++ and CMake can find the path.

    I did install Eigen following this official doc, which was why I was surprised the installation is still problematic.

    This answer solved my problem.

    This answer also solves the problem.

    On another note, my CMake file was a bit messed up, with both include_directories and target_link_libraries.