遇到的问题描述是在配置CMake时,尽管Eigen3已经安装在系统中,但是在尝试找到Pangolin包时,因为Pangolin的配置文件(
/usr/local/lib/cmake/Pangolin/PangolinConfig.cmake
)未能正确找到Eigen3的依赖,导致Pangolin被认为是未找到的。这个问题的根源在于CMake未能正确定位到Eigen3库,即使它已经安装在系统上。
报错界面:
-- Configuring incomplete, errors occurred!
CMake Deprecation Warning at CMakeLists.txt:1 (cmake_minimum_required):
Compatibility with CMake < 2.8.12 will be removed from a future version of
CMake.
Update the VERSION argument <min> value or use a ...<max> suffix to tell
CMake that the project does not need compatibility with older versions.
Build type: Release
-- Using flag -std=c++11.
CMake Error at CMakeLists.txt:40 (find_package):
Found package configuration file:
/usr/local/lib/cmake/Pangolin/PangolinConfig.cmake
but it set Pangolin_FOUND to FALSE so package "Pangolin" is considered to
be NOT FOUND. Reason given by package:
Pangolin could not be found because dependency Eigen3 could not be found.
-- Configuring incomplete, errors occurred!
Makefile:911: recipe for target 'cmake_check_build_system' failed
make: *** [cmake_check_build_system] Error 1
解决办法及原因:
解决这个问题的办法是在CMakeLists.txt
文件中,对find_package(Eigen3 REQUIRED)
命令添加NO_MODULE
选项,变更为:
find_package(Eigen3 REQUIRED NO_MODULE)
这个修改的具体原因和影响如下:
-
NO_MODULE参数的作用:
NO_MODULE
选项指示CMake优先使用Eigen3Config.cmake
(或findEigen3.cmake
)模块文件来查找Eigen3包。这是因为Eigen3提供了这样的配置文件,其中包含了Eigen3安装路径、库文件和其他必要的编译信息。使用这个配置文件,CMake可以更直接、更准确地找到Eigen3库。 -
为什么这样做可以解决问题:在没有指定
NO_MODULE
时,CMake可能会使用默认的查找逻辑,这可能不会考虑到所有安装路径或特定于Eigen3的配置细节。通过明确使用NO_MODULE
,CMake被引导使用Eigen3提供的配置文件,从而解决了路径或配置上的不匹配问题。 -
为什么Pangolin报错:Pangolin依赖于Eigen3,如果CMake在配置Pangolin时未能找到Eigen3,它就会报错说Pangolin找不到。这不是因为Pangolin本身未安装,而是因为它的一个关键依赖(Eigen3)未被正确识别。
-
重新编译的必要性:一旦
CMakeLists.txt
被修改,需要重新运行CMake配置过程和编译过程,以确保所有的更改都被应用。这个步骤确保了CMake可以根据最新的配置重新检测和定位所有依赖,从而成功编译项目。
通过这种方式,你可以解决由于依赖库定位问题导致的编译配置错误,保证项目能够正确地识别和使用系统中已安装的库。