【问题标题】:How do I include the Eigen library in a CMakelist.txt on windows如何在 Windows 上的 CMakelist.txt 中包含 Eigen 库
【发布时间】:2021-04-28 23:25:21
【问题描述】:

我正在尝试将 Eigen 库包含到我的 CMakelist.txt 中。我已按照 Eigen Docs 上的 CMake 说明进行操作,但我使用的是 Jetbrain 的 Clion 而不是直接使用 CMake。所以我不知道如何使用提供的 Cmake 命令。我进行了研究,但我对 CMake 的理解不是很好,无法编写 Cmakelists,所以我还没有得到任何工作。

这是我一直用来测试库的 serup 的:

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)

这是我收到的错误:

  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.

我研究了很多方法来包含该库,但大多数方法都使用我不熟悉的命令行。我也没有Eigen3Config.cmake 唯一的文件Eigen3Config.cmake.in。我假设有一些我必须不知道的安装技巧。如果有人有办法严格使用 CMakelist.txt 包含 clion,我将不胜感激。

【问题讨论】:

  • 那么,您究竟尝试了什么?您是否尝试过按照that answer 中的建议安装 Eigen3?这样Eigen3Config.cmake 文件就会出现。 (文件Eigen3Config.cmake.in 表明您在源中有 Eigen3)。您是否尝试过按照that answer 中的建议使用include_directories 命令而不是find_package
  • 我遇到了同样的问题,这里的答案是:stackoverflow.com/questions/59794643/… 是唯一对我有用的。删除 find_package、add_executable 和 target_link_libraries 命令并将它们替换为 set(EIGEN_DIR "path-to-extracted-eigen-zip-file") 和 include_directories(${EIGEN_DIR})。

标签: c++ cmake eigen clion


【解决方案1】:

这是一个在 Windows 上使用带有 mingw32-make.exe 和 g++.exe 编译器的 MinGW 环境的 CMake 工作示例。

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...)

然后可以调用 Eigen3 库,例如:

#include <Eigen/Core>

【讨论】:

    【解决方案2】:

    Eigen 是一个只有标头的库,因此您不必将它添加到 target_link_library,也不需要 CMake 宏来检测它。

    只需将头文件添加到您的包含路径中即可进行设置。

    【讨论】:

    • “而只是将头文件添加到包含路径中” - 哪个头文件以及如何?
    • 详情见此网址:eigen.tuxfamily.org/dox/GettingStarted.htmlIn 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.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-04
    • 2015-02-10
    • 2017-01-20
    • 1970-01-01
    • 2012-09-04
    • 1970-01-01
    相关资源
    最近更新 更多