How do I include the Eigen library in a CMakelist.txt on windows

Viewed 2046

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.

2 Answers

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>

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.

Related