Problem with including Eigen library in Clion CMake

不羁的心 提交于 2021-02-07 10:03:51

问题


I have a problem with the Eigen library. I use Clion on Linux and my project can't find the Eigen library (I have it in a folder on my desktop).

I have CMake in two configurations:

First:

cmake_minimum_required(VERSION 3.15)
project(TestFEM)

set(CMAKE_CXX_STANDARD 17)

set(EIGEN_DIR "~/Desktop/eigen-3.3.7")
include_directories(${EIGEN_DIR})

add_executable(TestFEM main.cpp FEM/FEM.cpp FEM/FEM.h)

And second:

cmake_minimum_required(VERSION 3.15)
project(TestFEM)

set(CMAKE_CXX_STANDARD 17)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}")
find_package(Eigen3 REQUIRED)
include_directories(${EIGEN3_INCLUDE_DIR})

add_executable(TestFEM main.cpp FEM/FEM.cpp FEM/FEM.h)

All the time, I have an error like this:

fatal error: Eigen\Dense: No such file or directory

How can I fix it?


回答1:


First, try using the full path to the Eigen directory (without ~).

set(EIGEN_DIR "/home/xxxx/Desktop/eigen-3.3.7")
include_directories(${EIGEN_DIR})

Also, check to be sure that path actually contains Eigen/Dense, so the full file path would be:

/home/xxxx/Desktop/eigen-3.3.7/Eigen/Dense

A better approach would be to use CMake to verify that path exists before using it:

set(EIGEN_DIR "/home/xxxx/Desktop/eigen-3.3.7")
if(NOT EXISTS ${EIGEN_DIR})
    message(FATAL_ERROR "Please check that the set Eigen directory is valid!")
endif()
include_directories(${EIGEN_DIR})

But you can be even more safe by verifying you are in the correct location within the Eigen repository by using find_path(). The Eigen repository has a dummy file signature_of_eigen3_matrix_library that you can use to verify you indeed found Eigen's top-level directory. Just use the PATHS clause to tell CMake where to look:

find_path(EIGEN_DIR NAMES signature_of_eigen3_matrix_library
    PATHS
    /home/xxxx/Desktop/eigen-3.3.7
    PATH_SUFFIXES eigen3 eigen
)
include_directories(${EIGEN_DIR})


来源:https://stackoverflow.com/questions/59794643/problem-with-including-eigen-library-in-clion-cmake

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!