Using Eigen in CMake Projects

这篇博客介绍了如何在CMake项目中轻松集成和使用Eigen库。首先,需要CMake3.0或更高版本,并通过find_package命令导入Eigen3::Eigen目标。接着,在CMakeLists.txt文件中设置target_link_libraries以链接Eigen。如果Eigen不在默认位置,需要设置CMAKE_PREFIX_PATH或Eigen3_DIR。示例代码展示了配置过程,包括创建可执行文件和处理非标准安装路径的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Using Eigen in CMake Projects

https://eigen.tuxfamily.org/dox/TopicCMakeGuide.html

Eigen provides native CMake support which allows the library to be easily used in CMake projects.

Note

CMake 3.0 (or later) is required to enable this functionality.

Eigen exports a CMake target called Eigen3::Eigen which can be imported using the find_package CMake command and used by calling target_link_libraries as in the following example:

cmake_minimum_required (VERSION 3.0)

project (myproject)

find_package (Eigen3 3.3 REQUIRED NO_MODULE)

add_executable (example example.cpp)

target_link_libraries (example Eigen3::Eigen)

The above code snippet must be placed in a file called CMakeLists.txt alongside example.cpp. After running

$ cmake path-to-example-directory

CMake will produce project files that generate an executable called example which requires at least version 3.3 of Eigen. Here, path-to-example-directory is the path to the directory that contains both CMakeLists.txt and example.cpp.

Do not forget to set the CMAKE_PREFIX_PATH variable if Eigen is not installed in a default location or if you want to pick a specific version. For instance:

$ cmake path-to-example-directory -DCMAKE_PREFIX_PATH=$HOME/mypackages

An alternative is to set the Eigen3_DIR cmake's variable to the respective path containing the Eigen3*.cmake files. For instance:

$ cmake path-to-example-directory -DEigen3_DIR=$HOME/mypackages/share/eigen3/cmake/

If the REQUIRED option is omitted when locating Eigen using find_package, one can check whether the package was found as follows:

find_package (Eigen3 3.3 NO_MODULE)

if (TARGET Eigen3::Eigen)

# Use the imported target

endif (TARGET Eigen3::Eigen)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值