运行ROS包时cmake报错及解决方法汇总(持续更新)
前言
最近在跑一个ros代码,在编译时就碰到了很多cmake报错,于是这几天都在改cmake报错。本文对这次编译过程中碰到的所有cmake报错进行汇总,并提供了相应的解决方法。这篇文章会持续更新,后续碰到的报错也会不断纳入进来。
一.编译方式的更新
以前一直使用catkin_make对ROS包进行编译,现在才发现这并不是最好的方法,应该使用catkin build (或快捷键Ctrl+Shift+b) 来编译。两种方式的区别在于:
catkin_make: 只能同时编译工作空间下的所有包,速度慢不灵活,初学时用。
catkin build (Ctrl+Shift+b): 可以选择编译所有包,也可以只编译一个指定的软件包,非常灵活
下面介绍catkin build的配置和使用。参考链接:catkin build 的使用-CSDN博客
1. 配置catkin build
执行以下命令:
sudo apt-get install python3-catkin-tools
如果有报错,显示“无法定位软件包”或有以下形式的错误:
E: Package 'python-catkin-tools' has no installation candidate
则先安装pip再安装catkin_tools,命令如下:
sudo apt-get update
sudo apt install python3-pip
sudo pip3 install -U catkin_tools
这样就可以安装成功了。
2. catkin build的用法
catkin build非常灵活,同时支持所有节点和一个包的编译。
编译所有节点:
输入 catkin build
或使用快捷键: Ctrl+Shift+b
单独编译一个包:
catkin build 包名
如果在开始编译时出现如下报错:
The build space at '/home/zhenghao/source/guard_ws/build' was previously built by 'catkin_make'. Please remove the build space or pick a different build space.
是因为之前使用的是catkin_make编译,现在catkin build与catkin_make冲突了。只需要把工作空间下的devel和build文件夹删除,重新编译即可。
SourceURL:file:///home/zhenghao/文档/RPS23赛季哨兵代码调研记录.docx
二. Ceres库报错
若catkin_make编译时出现以下报错:
CMake Error at CMakeLists.txt:88 (find_package):
By not providing "FindCeres.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "Ceres", but CMake did not find one.
Could not find a package configuration file provided by "Ceres" with any of the following names:
CeresConfig.cmake
ceres-config.cmake
Add the installation prefix of "Ceres" to CMAKE_PREFIX_PATH or set "Ceres_DIR" to a directory containing one of the above files. If "Ceres" provides a separate development package or SDK, be sure it has been installed.
则说明是Ceres库未安装。安装步骤如下:
1. 安装依赖项
sudo apt-get install liblapack-dev libsuitesparse-dev libcxsparse3 libgflags-dev libgoogle-glog-dev libgtest-dev
2. 下载源码
停留在主目录(总之不要进入工作区间!!)
克隆源码:
git clone https://github.com/ceres-solver/ceres-solver
3. 编译并安装
1)进入刚才下载过的ceres-solver文件
cd ceres-solver/
2)建立build文件并进入:
mkdir build
cd build
3)编译
cmake ..
make -j4
make test
4)安装
sudo make install
安装完成后即可。
注意:不要将ceres-solver文件下载到工作空间中!!否则会出现以下报错:
CMake Error at /opt/ros/noetic/share/catkin/cmake/catkin_workspace.cmake:100 (message):
This workspace contains non-catkin packages in it, and catkin cannot build a non-homogeneous workspace without isolation. Try the 'catkin_make_isolated' command instead.
Call Stack (most recent call first):
CMakeLists.txt:69 (catkin_workspace)
报错原因为catkin_ws/src里面包含了不是ROS(直接或间接)支持的包,不能用catkin_make或catkin build编译。而ceres-solver文件中可能包含不被ROS支持的包,因此不能将ceres-solver放在工作空间中,将其移动到工作空间以外(如主目录中)即可。
三. GTSAM安装
再次进行编译时若又出现以下报错:
CMake Error at guard_slam/slam_lib/STD/CMakeLists.txt:47 (find_package):
By not providing "FindGTSAM.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "GTSAM", but CMake did not find one.
Could not find a package configuration file provided by "GTSAM" with any of the following names:
GTSAMConfig.cmake
gtsam-config.cmake
Add the installation prefix of "GTSAM" to CMAKE_PREFIX_PATH or set "GTSAM_DIR" to a directory containing one of the above files. If "GTSAM" provides a separate development package or SDK, be sure it has been installed.
则说明未安装GTSAM。安装步骤如下:
1. 安装boost和cmake
安装boost:
sudo apt-get install libboost-all-dev
安装cmake:
sudo apt-get install cmake
2. 安装TBB
sudo apt-get install libtbb-dev
3. 安装MKL(full)
sudo apt-get install intel-mkl-full
4. 下载gtsam(同样不要下载到工作空间中)
git clone https://bitbucket.org/gtborg/gtsam.git
5. 编译
cd ~/gtsam
mkdir build
cd build
cmake ..
make check #可选的,运行单元测试
sudo make install
cmake .. 编译时可能出现以下报错:
CMake Error at wrap/CMakeLists.txt:29 (target_link_libraries):
The "debug" argument must be followed by a library.
或可能出现另一种报错:
CMake Error at gtsam/CMakeLists.txt:105 (target_link_libraries):
The "debug" argument must be followed by a library.
解决方法:修改CMakeLists.txt
找到gtsam/wrap/CMakeLists.txt 文件,把以下两块内容注释或者删除:
debug
${Boost_SERIALIZATION_LIBRARY_DEBUG}
${Boost_SYSTEM_LIBRARY_DEBUG}
${Boost_FILESYSTEM_LIBRARY_DEBUG}
${Boost_THREAD_LIBRARY_DEBUG}
${Boost_DATE_TIME_LIBRARY_DEBUG}
${Boost_REGEX_LIBRARY_DEBUG}
debug
${Boost_TIMER_LIBRARY_DEBUG}
${Boost_CHRONO_LIBRARY_DEBUG}
如果以上操作之后仍然无法解决问题,那么尝试以下另一种安装指令:
sudo add-apt-repository ppa:borglab/gtsam-release-4.0
sudo apt install libgtsam-dev libgtsam-unstable-dev
执行这两条指令后gtsam已经全部安装成功,不再需要后面的编译安装。
四. grid_map安装
再次输入catkin_make编译时若又出现以下报错:
CMake Error at/opt/ros/noetic/share/catkin/cmake/catkinConfig.cmake:83 (find_package):
Could not find a package configuration file provided by "grid_map_msgs" with any of the following names:
grid_map_msgsConfig.cmake
grid_map_msgs-config.cmake
Add the installation prefix of "grid_map_msgs" to CMAKE_PREFIX_PATH or set "grid_map_msgs_DIR" to a directory containing one of the above files. If "grid_map_msgs" provides a separate development package or SDK, be sure it has been installed.
则说明未安装grid_map,系统中无法定位依赖包:ros-noetic-grid-map。 输入以下命令:
sudo apt-get install ros-noetic(ros版本)-grid-map
或输入以下命令(仅适用于ROS):
sudo apt-get install ros-noetic(ros版本)-grid-map-*
或者执行以下命令直接克隆grid_map源代码:
1. 获取源码
可以从 grid_map 的官方 GitHub 仓库获取源码。
git clone https://github.com/anybotics/grid_map.git
2. 安装依赖项
根据 grid_map 的文档,安装其依赖项。这可能包括 ROS、PCL(Point Cloud Library)等。
sudo apt-get install ros-<your-ros-distribution>-pcl-ros
3. 编译和安装:
cd grid_map
mkdir build
cd build
cmake ..
make
sudo make install
注意:编译cmake ..时一定要在有CMakeLists.txt文件的目录下编译,否则会报错。
五. serial库安装
再次输入catkin_make编译若又出现以下报错:
CMake Error at/opt/ros/noetic/share/catkin/cmake/catkinConfig.cmake:83 (find_package):
Could not find a package configuration file provided by "serial" with any of the following names:
serialConfig.cmake
serial-config.cmake
Add the installation prefix of "serial" to CMAKE_PREFIX_PATH or set "serial_DIR" to a directory containing one of the above files. If "serial" provides a separate development package or SDK, be sure it has been installed.
则说明未安装serial库。
若是kinetic或melodic版本,可以执行以下命令:
sudo apt-get install ros-kinetic-serial
或 sudo apt-get install ros-melodic-serial
但如果是noetic版本执行sudo apt-get install ros-noetic-serial命令后可能出现以下报错:
E: Unable to locate package ros-noetic-serial
该方法不可行,noetic版本集成serial包需要自己进行源码编译安装,只能通过下载serial源代码的方式。
执行以下命令克隆serial源代码:
cd 工作空间/src
git clone https://github.com/wjwwood/serial.git
进入serial目录编译,安装:
cd serial
make
make install
当其他功能包需要调用serial时,需要进行以下配置:
1. 在serial文件夹下对CMakeLists.txt进行修改:
## Include headers
include_directories(include)
## 改成
## Include headers
include_directories(include ${catkin_INCLUDE_DIRS})
2. 在需要serial的功能包下的package.xml中添加
<build_depend>serial</build_depend>
<build_export_depend>serial</build_export_depend>
3. 在需要serial的功能包的CMakeLists.txt中的find_package语句中添加serial依赖
find_package(catkin REQUIRED COMPONENTS
roscpp
serial
)
4. 添加头文件路径
## Include headers
include_directories(include ${catkin_INCLUDE_DIRS})
六. 相同文件重复问题
将以上所有包安装好后再进行编译,可能出现以下报错(或相同格式同一类型仅文件路径不同的报错):
CMake Error at guard_slam/slam_lib/FAST_LIO_LC/FAST-LIO/CMakeLists.txt:87 (add_executable):
add_executable cannot create target "fastlio_mapping" because another target with the same name already exists. The existing target is an executable created in source directory
"/home/zhenghao/source/guard_ws/src/guard_slam/slam_lib/FAST_LIO". See documentation for policy CMP0002 for more details.
说明有两个名称相同的文件同时经过了可执行设置,有冲突。此时检查一下两个重名文件内容是否相同。若不相同考虑改名,但同时也要改变CMakeLists.txt配置中所有对应的文件名称。若相同,不要删除任何一个文件,将不需要的文件的可执行设置及其他所有配置在其所在功能包的CMakeLists.txt中注释掉即可。类似下面:
# add_executable(pointcloud_publisher_node
# src/pointcloud_publisher_node.cpp
# )
# add_dependencies(pointcloud_publisher_node
# ${PROJECT_NAME}
# )
# target_include_directories(pointcloud_publisher_node PRIVATE
# include
# )
# target_include_directories(pointcloud_publisher_node SYSTEM PUBLIC
# ${catkin_INCLUDE_DIRS}
# ${EIGEN3_INCLUDE_DIR}
# )
# target_link_libraries(pointcloud_publisher_node
# ${PROJECT_NAME}
# ${catkin_LIBRARIES}
# )
七. 设定C++版本
若再次编译时出现了很多类似下面形式的报错:
/usr/include/pcl-1.10/pcl/point_types.h:698:1: error: ‘mulscalar’ is not a member of ‘pcl::traits’
698 | POINT_CLOUD_REGISTER_POINT_STRUCT (pcl::_PointDEM,
错误原因大概率是编译时没有设定C++编译器版本,需要在一级src文件下的CMakeLists.txt中设定。在CMakeLists.txt中project(Project)和set(CATKIN_TOPLEVEL TRUE)两句语句中间添加如下语句即可:
set(CMAKE_CXX_STANDARD 14)
不要插在文件最后,插在最后无效!
另外由于此处的CMakeLists.txt文件属于受保护文件,修改保存时右下角会弹出权限不足提示,选择以超级用户身份认证即可。
八. 安装boost库
此时再编译如果又出现如下报错:
/opt/ros/noetic/include/pcl_ros/point_cloud.h:231:15: error: there are no arguments to ‘BOOST_FOREACH’ that depend on a template parameter, so a declaration of ‘BOOST_FOREACH’ must be available [-fpermissive]
231|BOOST_FOREACH(constpcl::detail::FieldMapping& fm, mapping) {
这个错误是由于缺少对BOOST_FOREACH的声明导致的。BOOST_FOREACH是一个用于遍历容器的宏,它在C++标准库中没有定义,需要使用Boost库来提供支持。也就是说首先需要安装boost库。安装步骤如下:
1. 下载boost的tar文件
进入boost官网下载:https://www.boost.org/
当前boost官网最新版本为1_84_0,下载该版本的tar文件boost_1_84_0.tar.gz即可。
2. 解压文件
在终端输入如下解压指令:
tar -xzvf boost_1_84_0.tar.gz
3. 执行脚本文件
进入该文件夹目录下,打开终端输入并执行如下脚本:
sudo ./bootstrap.sh
4. boost安装
输入以下指令:
sudo ./b2 install
安装完毕。
九. 安装Eigen3库
换了一种编译方式,用catkin build(Ctrl+Shift+b)编译后出现以下错误:
CMake Error at/opt/ros/noetic/share/catkin/cmake/catkinConfig.cmake:83 (find_package):
Could not find a package configuration file provided by "Eigen3" 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.
则说明未安装Eigen3库。执行以下命令:
sudo apt-get install libeigen3-dev
也可以用源码编译安装,步骤如下:
#克隆源码
git clone https://github.com/eigenteam/eigen-git-mirror
#安装
cd eigen-git-mirror(后来卸载了装了eigen-3.3.7,命令也要相应改一下)
mkdir build
cd build
cmake ..
sudo make install
#安装后,头文件安装在/usr/local/include/eigen3/
#移动头文件
sudo cp -r /usr/local/include/eigen3/Eigen /usr/local/include
若操作后再编译仍然会报相同的错误,那就多卸载再安装几次,可能有用。
Eigen3卸载方式如下:
如果是用命令行安装的,用以下指令卸载:
sudo apt-get remove libeigen3-dev
如果是下载源代码手动编译安装的,用以下指令卸载:
cd /path/to/eigen3/source
sudo make uninstall
再编译可能报以下错误:
CMake Error at /opt/ros/noetic/share/costmap_2d/cmake/costmap_2dConfig.cmake:113 (message):
Project 'costmap_2d' specifies '/usr/include/eigen3' as an include dir, which is not found. It does neither exist as an absolute directory nor in '${{prefix}}//usr/include/eigen3'. Check the website 'http://wiki.ros.org/costmap_2d' for information and consider reporting the problem.
错误原因是CMake没有找到Eigen。Eigen的安装位置是/usr/local/include/eigen3而ROS系统中Eigen的默认安装位置是/usr/include/eigen3.很显然系统在旧的位置寻找eigen包。
执行以下语句:
sudo ln -s /usr/local/include/eigen3 /usr/include/eigen3
即可改变Eigen3的路径。
如果上面的方法全部尝试一遍后仍然会报原来的错误,就先将eigen3卸载,按照以下步骤走一遍,一般问题就能得到解决(我是到这一步才解决的):
1. 确认Eigen3是否已安装
用以下命令来安装Eigen3:
sudo apt-get update
sudo apt-get install libeigen3-dev
2. 查找Eigen3的安装位置
使用find命令来查找Eigen3Config.cmake或eigen3-config.cmake的确切位置:
sudo find /usr/ -name "Eigen3Config.cmake"
sudo find /usr/ -name "eigen3-config.cmake"
注意:如果你从源代码安装Eigen3或安装到了自定义路径,你需要相应地调整上述命令来搜索正确的目录。
3. 设置Eigen3_DIR环境变量
一旦找到了Eigen3的cmake配置文件,你需要将其路径设置到Eigen3_DIR环境变量中,这样CMake就能在构建过程中找到它。打开终端,根据实际路径设置变量(以下以我的系统中找到的路径为例):
export Eigen3_DIR=/usr/local/share/eigen3
4. 修改CMakeLists.txt
如果上述方法仍然无法解决问题,你可能需要编辑出现问题的ROS软件包(即需要引用Eigen3的软件包)中的CMakeLists.txt文件,直接添加查找Eigen3的指令和包含目录。打开CMakeLists.txt,确保包含以下内容:
find_package(Eigen3 REQUIRED)
include_directories(${EIGEN3_INCLUDE_DIR})
5. 设置CMAKE_PREFIX_PATH或设置 Eigen3_DIR
1)设置CMAKE_PREFIX_PATH
在用到了Eigen3的软件包中的CMakeLists.txt文件中改动。在调用 find_package 前,添加如下一行代码:
set(CMAKE_PREFIX_PATH "/path/to/eigen3;${CMAKE_PREFIX_PATH}")
其中/path/to/eigen3要替换为自己的Eigen3安装路径,比如我的系统中Eigen3安装路径为:/usr/local/share/eigen3
2)设置 Eigen3_DIR
同样在用到了Eigen3的软件包中的CMakeLists.txt文件中改动。同样在调用 find_package 前,添加如下一行代码,直接设置 Eigen3_DIR 变量,告诉 CMake Eigen3 的配置文件所在路径:
set(Eigen3_DIR "/path/to/eigen3/share/eigen3/cmake")
其中/path/to/eigen3/share/eigen3/cmake要替换为自己的Eigen3 安装路径中的 cmake 文件夹所在路径,比如在我的系统中应为:/usr/local/share/eigen3/cmake
6. 重新构建ROS工作空间(重新编译一次)
完成以上修改后,回到你的ROS工作空间目录,运行catkin build(或Ctrl+Shift+b)重新编译:
cd 工作空间路径
Ctrl+Shift+b
完成以上所有步骤大概率能解决这个报错。
十. 安装costmap_2d
编译时也可能会出现以下报错:
CMake Error at /opt/ros/noetic/share/catkin/cmake/catkinConfig.cmake:85 (find_package):
Could not find a package configuration file provided by "costmap_2d" with any of the following names:
costmap_2dConfig.cmake
costmap_2d-config.cmake
Add the installation prefix of "costmap_2d" to CMAKE_PREFIX_PATH or set "costmap_2d_DIR" to a directory containing one of the above files. If "costmap_2d" provides a separate development package or SDK, be sure it has been installed.
则说明未安装costmap_2d。安装及解决该报错的完整步骤如下:
1. 安装costmap_2d包
执行以下命令安装:
sudo apt-get install ros-noetic-costmap-2d
后面的操作需要用到这个包的路径,查找路径可输入以下命令:
rospack find 被查找的包名
比如这里是 rospack find costmap_2d
查找结果为 /opt/ros/noetic/share/costmap_2d
2. 设置CMAKE_PREFIX_PATH:
如果你已经安装了"costmap_2d"包但仍然遇到问题,可能是因为CMake无法找到包的安装位置。在这种情况下,你可以手动设置CMAKE_PREFIX_PATH环境变量来指定包的安装路径。在终端中执行以下命令:
export CMAKE_PREFIX_PATH=/opt/ros/noetic/share/costmap_2d:$CMAKE_PREFIX_PATH
这会将"costmap_2d"包的安装路径添加到CMake的搜索路径中。
3. 设置costmap_2d_DIR:
如果上述步骤没有解决问题,你可以尝试设置costmap_2d_DIR变量来指定包含"costmap_2dConfig.cmake"或"costmap_2d-config.cmake"文件的目录。通常情况下,通常"costmap_2d"包的路径为/opt/ros/noetic/share/costmap_2d/cmake。在终端中执行以下命令:
export costmap_2d_DIR=/opt/ros/noetic/share/costmap_2d/cmake
4. 重新运行CMake(重新编译):
在应用了上述更改后,重新运行你的CMake命令。这应该会告诉CMake去新的路径中查找"costmap_2d"包,并且应该不再出现找不到配置文件的错误。报错解决。
十一. 安装pcl_ros包
若编译时出现以下报错:
CMake Error at /opt/ros/noetic/share/catkin/cmake/catkinConfig.cmake:85 (find_package):
Could not find a package configuration file provided by "pcl_ros" with any of the following names:
pcl_rosConfig.cmake
pcl_ros-config.cmake
Add the installation prefix of "pcl_ros" to CMAKE_PREFIX_PATH or set "pcl_ros_DIR" to a directory containing one of the above files. If "pcl_ros" provides a separate development package or SDK, be sure it has been installed.
则说明未安装pcl_ros包。执行以下命令进行安装:
sudo apt-get install ros-noetic-pcl-ros
安装后一般就能解决这个报错,若还会出现同样的报错,那么与上面两个报错的解决方法雷同,按照 设置CMAKE_PREFIX_PATH/设置pcl_ros_DIR/重新编译 的顺序走一遍即可。
十二. 安装eigen_conversions包
若编译时出现以下报错:
CMake Error at /opt/ros/noetic/share/catkin/cmake/catkinConfig.cmake:85 (find_package):
Could not find a package configuration file provided by "eigen_conversions" with any of the following names:
eigen_conversionsConfig.cmake
eigen_conversions-config.cmake
Add the installation prefix of "eigen_conversions" to CMAKE_PREFIX_PATH or set "eigen_conversions_DIR" to a directory containing one of the above files. If "eigen_conversions" provides a separate development package or SDK, be sure it has been installed.
则说明未安装eigen_conversions包。执行以下命令进行安装:
sudo apt-get install ros-noetic-eigen-conversions
安装后一般就能解决这个报错,若还会出现同样的报错,那么与上面几个报错的解决方法雷同,按照 设置CMAKE_PREFIX_PATH/设置eigen_conversions_DIR/重新编译 的顺序走一遍即可。
十三. 更新ROS(重新安装一下)
若出现类似下面的报错:
/home/zhenghao/source/guard_ws/src/guard_slam/slam_lib/FAST_LIO_LC/FAST-LIO/src/preprocess.h:4:10: fatal error: livox_ros_driver/CustomMsg.h: 没有那个文件或目录
4 | #include <livox_ros_driver/CustomMsg.h>
或者在安装完某个包后突然多出了很多类似前面原因是有包未安装的报错,比如前面所出现的costmap_2d/pcl_ros/eigen_conversions的报错,则说明很可能是上面安装一系列包和库时一些ROS中原有的包被删掉了。所以要重新装一下ROS,输入以下命令:
sudo apt update
sudo apt install ros-noetic-desktop-full
这样就能找回丢失的ROS包。
当然也可以选择针对报错逐个安装,安装方法大同小异,与前面costmap_2d/pcl_ros/eigen_conversions包的安装方法类似,参考即可。
十四. 添加queue头文件
编译时若出现类似下面这样的报错:
error: ‘priority_queue’ is not a member of ‘std’
1680 | std::priority_queue<float> pq;
该错误表明在代码中std::priority_queue 类型不被识别为 std 命名空间的一部分,极有可能缺少某个头文件。如此处缺少的是#include <queue>
加上后一般就没问题了。
十五. 找不到匹配函数的问题
前几天有一个一直攻克不下的报错,如今终于克下了!!报错如下:
error: no matching function for call to ‘std::shared_ptr<pcl::PointClouddlio::Point >::shared_ptr(boost::detail::sp_if_not_array<pcl::PointClouddlio::Point >::type)’ 1766 | pcl::PointCloud<PointType>::Ptr submap_cloud_ (boost::make_shared<pcl::PointCloud<PointType>>());
问题描述:该报错表明在调用 std::shared_ptr
的构造函数时没有找到匹配的函数。具体来说,它指的是在创建 std::shared_ptr
对象时,找不到适合给定参数类型的构造函数。这是一个代码内部的问题,不是配置问题。
仔细检查了一下下面这行代码:
pcl::PointCloud<PointType>::Ptr submap_cloud_ (boost::make_shared<pcl::PointCloud<PointType>>());
最终发现问题出在boost::make_shared上,在C++11及更高版本的标准库中有std::make_shared,而如果要用
boost::make_shared,就要保证boost版本足够新。显然我的boost版本不够,不能用boost,改法就是将boost改成std,全部替换成std::make_shared。再编译就没有报错了。
十六.tf2安装
如果出现以下报错:
fatal error: tf2_sensor_msgs/tf2_sensor_msgs.h: 没有那个文件或目录
5 | #include <tf2_sensor_msgs/tf2_sensor_msgs.h>
说明是tf2_sensor_msgs库没有安装,执行以下语句对该库进行安装:
sudo apt-get install ros-<ros version>-tf2-sensor-msgs
即可解决该问题。
更多推荐
所有评论(0)