4

I am trying to include the Eigen library to my CMakelist.txt. I have followed the CMake instructions on the Eigen Docs but I am using Jetbrain's Clion and not CMake directly. So I do not know how to use the Cmake commands provided. I have researched around but I don't have have a very good understanding of CMake to write Cmakelists, so I haven't been able to get anything to work yet.

this is what I have been using just to test the serup of the library:

cmake_minimum_required(VERSION 3.17)
project(Eigen_Test)
set(CMAKE_CXX_STANDARD 20)

find_package (Eigen3 3.3 REQUIRED NO_MODULE)

add_executable (example example.cpp)
target_link_libraries (example eigen)

add_executable(Eigen_Test main.cpp)

this is the error I have been receiving:

  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.

I Have researched many ways to include the library but most methods use command lines which I am unfamiliar with. Also I do not have an Eigen3Config.cmake the only file I have Eigen3Config.cmake.in. I assume there is some install trick that I must not be aware of. If anyone has a way to include clion strictly using a CMakelist.txt, I would be greatly appreciative.

Mike
  • 41
  • 1
  • 3
  • So, what **exactly** have you tried? Have you tried to **install** Eigen3, as suggested in [that answer](https://stackoverflow.com/questions/31774034/could-not-find-cmake-package-configuration-file-for-eigen3)? That way `Eigen3Config.cmake` file will appear. (The file `Eigen3Config.cmake.in` indicates that you have Eigen3 in sources). Have you tried to use `include_directories` command instead of `find_package`, as suggested in [that answer](https://stackoverflow.com/a/12258855/3440745)? – Tsyvarev Sep 18 '20 at 08:14
  • I had the same issue and the answer here: https://stackoverflow.com/questions/59794643/problem-with-including-eigen-library-in-clion-cmake is the only one that worked for me. Remove the find_package, add_executable and target_link_libraries commands and replace them with set(EIGEN_DIR "path-to-extracted-eigen-zip-file") and include_directories(${EIGEN_DIR}). – randomuser Jan 11 '21 at 17:00

2 Answers2

4

Here a working example with CMake on Windows using the MinGW environment with mingw32-make.exe and g++.exe compiler.

CMakeLists.txt :

# The following lines depends on your project :
cmake_minimum_required(VERSION 3.19)
project(PROJECT_NAME)
set(CMAKE_CXX_STANDARD 17)

# You have to set these variables as Windows environment variables:
# EIGEN3_INCLUDE_DIR <- %EIGEN3_ROOT%
# EIGEN3_DIR <- %EIGEN3_ROOT%\cmake
#
# EIGEN3_INCLUDE_DIR: variable needed for file %EIGEN3_ROOT%/cmake/FindEigen3.cmake
#
# CMAKE_MODULE_PATH: Search path for the module Eigen3 to be loaded by find_package
#
SET( EIGEN3_INCLUDE_DIR "$ENV{EIGEN3_INCLUDE_DIR}" )
SET( CMAKE_MODULE_PATH "$ENV{EIGEN3_DIR}" )

find_package( Eigen3 3.3 REQUIRED )
# include_directories is needed for the compiler to know where looking for Eigen3 header files to be included 
include_directories( ${EIGEN3_INCLUDE_DIR} )

add_executable(PROJECT_NAME FILES...)

You can then call the Eigen3 libraries, such as:

#include <Eigen/Core>
-1

Eigen is a header only library, so you don't have to add it to target_link_library, and you don't need a CMake Macro to detect it.

Instead just add the header file into your include path and you should be set.

arved
  • 4,401
  • 4
  • 30
  • 53
  • 2
    "Instead just add the header file into your include path" - which header file and how? – randomuser Jan 11 '21 at 16:13
  • See this URL for details: https://eigen.tuxfamily.org/dox/GettingStarted.html ```In order to use Eigen, you just need to download and extract Eigen's source code (see the wiki for download instructions). In fact, the header files in the Eigen subdirectory are the only files required to compile programs using Eigen. The header files are the same for all platforms. It is not necessary to use CMake or install anything.``` – arved Jan 12 '21 at 08:23