Error with Clion/Cmake and Eigen

2019-07-27 03:06发布

问题:

I am trying to get Eigen up and running but I am running into a roadblock. I opened Clion and in the CMakeLists.txt tab I entered the following code. Please note I have installed Eigen with home-brew.

project(untitled)

cmake_minimum_required(VERSION 3.7)

set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

find_package(Eigen3 REQUIRED)

include_directories(EIGEN3_INCLUDE_DIR)

set(SRCS main.cpp)

add_executable(untitled ${SRCS})

I am getting the following error.

/Applications/CLion.app/Contents/bin/cmake/bin/cmake -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - Unix Makefiles" /Users/johnmcconnell/CLionProjects/untitled
CMake Error at CMakeLists.txt:9 (find_package):
  Found package configuration file:

    /usr/local/share/eigen3/cmake/Eigen3Config.cmake

  but it set Eigen3_FOUND to FALSE so package "Eigen3" is considered to be
  NOT FOUND.

I've never done this before and I am really at a loss, any ideas on what to do?

UPDATE: Change in code clears the error but yields a new one.

fatal error: 'Eigen/Dense' file not found

project(untitled)

cmake_minimum_required(VERSION 3.7)

set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)

include_directories(EIGEN_INCLUDE_DIR)

set(SRCS main.cpp)

add_executable(untitled ${SRCS})

回答1:

In addition to the problem solved by @John Zwinck's answer, you have an error in your include_directories statement.

It should be

include_directories(${EIGEN3_INCLUDE_DIR})

instead of

include_directories(EIGEN_INCLUDE_DIR)


回答2:

If you look at the source code of the CMake module: https://github.com/RLovelett/eigen/blob/master/cmake/FindEigen3.cmake

You can see that it accepts "hints" in either EIGEN3_ROOT or EIGEN3_ROOT_DIR. Set one of those to the base of your Eigen installation and try again:

EIGEN3_ROOT=blah/blah cmake ...


回答3:

  • Include the following in CMakeLists.txt

    set(EIGEN_DIR "C:\\Eigendir\\Eigen")
    include_directories(${EIGEN_DIR})
    add_executable(project_name main.cpp)
    target_link_libraries(project_name ${EIGEN_DIR})
    
  • The directory in which you placed Eigen's source code must be in the include path

  • EIGEN_DIR is set to the path where the Eigen folder is in your system
  • This worked for me and do not forget the #include <Eigen> in the files where ever you are using Eigen... Hope this answer helps you :D


标签: cmake eigen