不幸的是,我在 Ubuntu 18.04.4 LTS 系统上使用 Cmake将只有Eigen 3.3.7 库的标头添加到我的 Makefile 时遇到了一些问题。我可以使用库编译我的代码,只需复制包含目录中的库文件夹并
include_directories(./include/eigen3)
在 CMakeLists.txt 文件中使用。但是,我更希望不对库路径进行硬编码,而是能够在 CMakeLists.txt 文件中动态设置它,以便更轻松地与其他人共享项目。不幸的是,我找到的所有说明都没有对我有用。
我准备了以下最小代码示例:
主.cpp:
#include <eigen3/Eigen/Dense>
#include <iostream>
int main()
{
Eigen::Vector3d test_vec(1.0f, 2.0f, 3.0f);
std::cout << test_vec << std::endl;
return 0;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.0)
project(Eigen-Cmake-Test VERSION 1.0) # set the project name
find_package (Eigen3 3.3 REQUIRED NO_MODULE)
include_directories(${EIGEN_INCLUDE_DIR})
# add the executable
add_executable("${PROJECT_NAME}" ./main.cpp)
target_link_libraries("${PROJECT_NAME}" Eigen3::Eigen)
我只下载了标题Eigen 3.3.7 Library并将文件夹重命名为 eigen3。然后将该文件夹移动到:
/usr/local/share/eigen3
当我运行时,cmake CMakeLists.txt
我收到以下错误:
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.
-- Configuring incomplete, errors occurred!
当我检查 Eigen 库文件夹时,我意识到/usr/local/share/eigen3/cmake
它只包含一个名为 Eigen3Config.cmake.in 的文件,而不是必需的 Eigen3Config.cmake。为什么会这样?
我尝试将文件重命名为 Eigen3Config.cmake。这里的错误如下:
CMake Error at CMakeLists.txt:5 (find_package):
Could not find a configuration file for package "Eigen3" that is compatible
with requested version "3.3".
The following configuration files were considered but not accepted:
/usr/local/share/eigen3/cmake/Eigen3Config.cmake, version: unknown
-- Configuring incomplete, errors occurred!
此外,我还尝试了解释stackoverflow 的解决方案:CMake find_package not working for Eigen? 没有成功。
我怎样才能使它正常工作?另外我认为我还不太了解底层系统。任何解释将不胜感激。