使用eigen和cmake

时间:2018-04-03 12:13:11

标签: c++ cmake eigen

我对C ++和CMake相对较新,并试图在我当前的CMake项目中使用eigen库,但我根本无法让它工作。

我搜索了Stackoverflow和谷歌,但没有找到适合我的解决方案。

我当前的目录结构

.
├── CMakeLists.txt
├── README.md
├── bin
├── build
├── main.cc
├── src
│   ├── CMakeLists.txt
│   ├── (other files)
│   ├── infra
│   │   └── (other files)
│   ├── lib
│   │   └── eigen (clone of the git repo)
│   │       ├── CMakeLists.txt
│   │       ├── Eigen (source files)
│   │       └── (other files) 
│   └── util
│       └── (other files)
│       
└── tests
    └──  (other files)

./的CMakeLists.txt

cmake_minimum_required(VERSION 3.9)

find_program(CMAKE_C_COMPILER NAMES $ENV{CC} gcc PATHS ENV PATH NO_DEFAULT_PATH)
find_program(CMAKE_CXX_COMPILER NAMES $ENV{CXX} g++ PATHS ENV PATH NO_DEFAULT_PATH)

set(CMAKE_VERBOSE_MAKEFILE ON)

set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_HOME_DIRECTORY}/bin)
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_HOME_DIRECTORY}/build)

set (CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")

project(myProject VERSION 0.0.1)

#For Testing
include(CTest)
enable_testing()

SET(SOURCE_FILES main.cc)
add_executable(${CMAKE_PROJECT_NAME}_run ${SOURCE_FILES})

include_directories(src)
add_subdirectory(src)

target_link_libraries(${CMAKE_PROJECT_NAME}_run ${CMAKE_PROJECT_NAME}_lib)

./ SRC /的CMakeLists.txt

project(${CMAKE_PROJECT_NAME}_lib)

# Clone Eigen
set(EXTERNAL_DIR ${CMAKE_SOURCE_DIR}/src/lib/)
if(NOT EXISTS ${EIGEN_DIR})
    find_package(Git REQUIRED)
    execute_process(
            COMMAND ${GIT_EXECUTABLE} clone https://github.com/eigenteam/eigen-git-mirror ${EXTERNAL_DIR}eigen
    )
endif()

    set(HEADER_FILES
           myHeaders
    )

    set(SOURCE_FILES
            mySources
    )


    include_directories(lib/eigen/Eigen/)

    add_library(${CMAKE_PROJECT_NAME}_lib STATIC ${SOURCE_FILES} ${HEADER_FILES})

对于编译我在使用Home-brew安装的7.3.0_1版本中使用gccg++

由于它只是一个标题库,我认为只使用include_directories()就足够了。我也尝试使用http://eigen.tuxfamily.org/dox/TopicCMakeGuide.html中提到的官方方法,但这也没有用。

使用当前配置,我收到很多错误,看起来像这样(只是一个小摘录):

    **/evsr/src/lib/eigen/Eigen/src/Core/Matrix.h:18:8: error: 'traits' is not a class template
 struct traits<Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
        ^~~~~~
**/evsr/src/lib/eigen/Eigen/src/Core/Matrix.h:18:15: error: 'Matrix' was not declared in this scope
 struct traits<Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
               ^~~~~~
**/evsr/src/lib/eigen/Eigen/src/Core/Matrix.h:18:15: note: suggested alternative: 'atoi'
 struct traits<Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
               ^~~~~~
               atoi
**/evsr/src/lib/eigen/Eigen/src/Core/Matrix.h:18:75: error: expected unqualified-id before '>' token
 struct traits<Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
                                                                           ^
In file included from **/evsr/src/infra/random_projection.cc:9:0:
**/evsr/src/lib/eigen/Eigen/src/Core/Matrix.h:179:27: error: expected template-name before '<' token
   : public PlainObjectBase<Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
                           ^
**/evsr/src/lib/eigen/Eigen/src/Core/Matrix.h:179:27: error: expected '{' before '<' token
**/evsr/src/lib/eigen/Eigen/src/Core/Matrix.h:428:32: error: wrong number of template arguments (3, should be 6)
 typedef Matrix<Type, Size, Size> Matrix##SizeSuffix##TypeSuffix;  \
                                ^
**/evsr/src/lib/eigen/Eigen/src/Core/Matrix.h:441:1: note: in expansion of macro 'EIGEN_MAKE_TYPEDEFS'
 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 2, 2) \
 ^~~~~~~~~~~~~~~~~~~
**/evsr/src/lib/eigen/Eigen/src/Core/Matrix.h:449:1: note: in expansion of macro 'EIGEN_MAKE_TYPEDEFS_ALL_SIZES'
 EIGEN_MAKE_TYPEDEFS_ALL_SIZES(int,                  i)
 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~

也许某人遇到过这个或类似的问题并且知道答案。我为每一个小小的笔记感到高兴。

1 个答案:

答案 0 :(得分:0)

我能够通过在Eigen中添加src/CMakeLists.txt标头文件的绝对路径来解决此问题。

include_directories(${CMAKE_SOURCE_DIR}/src/lib/eigen/)

我不确定为什么这会解决这个问题,但确实如此。