无需SLAM建图,直接生成Gazebo仿真环境的二维代价地图的功能包pgm_map_creator适配ROS Noetic编译报错及解决方法

   在ROS Noetic中,使用pgm_map_creator功能包直接生成Gazebo仿真环境的二维代价地图时,可能会遇到各种编译和运行错误。本篇博客详细解析了在适配ROS Noetic过程中常见的报错及其解决方法,包括Boost文件结构更新、C++版本兼容性、Gazebo函数名变更、链接库缺失以及protobuf文件加载错误。通过逐步排查和修改,如更换头文件路径、调整C++标准、更新CMakeLists.txt文件等,成功解决了这些问题,使得pgm_map_creator功能包能够顺利编译和运行。本篇文章不仅提供了详尽的错误分析和解决方案,还附带了相关指令和示例图片,帮助读者快速定位并解决类似问题。

   一、第一类错误boost文件结构更新导致的 boost … 没有那个文件或目录

   报错 1、 fatal error: boost/gil/gil_all.hpp: 没有那个文件或目录

[ 76%] Building CXX object pgm_map_creator-master/CMakeFiles/collision_map_creator.dir/src/collision_map_creator.cc.o
/home/gly/catkin_ws/src/pgm_map_creator-master/src/collision_map_creator.cc:4:10: fatal error: boost/gil/gil_all.hpp: 没有那个文件或目录
4 | #include <boost/gil/gil_all.hpp>
| ^~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make[2]: *** [pgm_map_creator-master/CMakeFiles/collision_map_creator.dir/build.make:63:pgm_map_creator-master/CMakeFiles/collision_map_creator.dir/src/collision_map_creator.cc.o] 错误 1
make[1]: *** [CMakeFiles/Makefile2:440:pgm_map_creator-master/CMakeFiles/collision_map_creator.dir/all] 错误 2
make: *** [Makefile:141:all] 错误 2
Invoking “make -j2 -l2” failed

   报错 2、fatal error: boost/gil/extension/io/png_dynamic_io.hpp: 没有那个文件或目录

[ 84%] Building CXX object pgm_map_creator-master/CMakeFiles/request_publisher.dir/src/request_publisher.cc.o
/home/gly/catkin_ws/src/pgm_map_creator-master/src/collision_map_creator.cc:6:10: fatal error: boost/gil/extension/io/png_dynamic_io.hpp: 没有那个文件或目录
6 | #include <boost/gil/extension/io/png_dynamic_io.hpp>
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make[2]: *** [pgm_map_creator-master/CMakeFiles/collision_map_creator.dir/build.make:63:pgm_map_creator-master/CMakeFiles/collision_map_creator.dir/src/collision_map_creator.cc.o] 错误 1
make[1]: *** [CMakeFiles/Makefile2:440:pgm_map_creator-master/CMakeFiles/collision_map_creator.dir/all] 错误 2
make[1]: *** 正在等待未完成的任务…

   第一类错误(报错1和报错2)的报错原因及解决方法:

   (1)依次执行以下两条语句排查是否已经安装了libboost

sudo apt-get update
sudo apt-get install libboost-all-dev

   没安装的小伙伴安装一下,从下图可以看出来,我之前已经装过了
在这里插入图片描述

   (2)使用find指令分别在boost中查找报错1中提到的带gil的文件、报错2中提到的boost/gil/extension/io中的hpp文件,检查是否分别存在gil.hpp和png.hpp

find /usr/include/boost -name "gil*"
find /usr/include/boost/gil/extension/io -name "*.hpp"

   从下图可以看出,这两个文件均存在

   (3)更换调用的头文件

   报错1和报错2的原因,实际上是因为随着Boost 版本的更新,Boost GIL库的头文件路径或名称有所变化。功能包pgm_map_creator中的调用的以下头文件,已经不适用于新版本的Boost,文件不存在,当然找不到

#include <boost/gil/gil_all.hpp>
#include <boost/gil/extension/io/png_dynamic_io.hpp>

   经过上面第(1)步和第(2)的检查,我们已经确定安装了Boost,并且存在可替换的文件gil.hpp和png.hpp,所以只需要将功能包pgm_map_creator的collision_map_creator.cc文件中的以上头文件更换为以下的文件,即可解决报错1和报错2

#include <boost/gil.hpp>
#include <boost/gil/extension/io/png.hpp>

   二、第二类错误 C++版本导致的编译错误

   报错 3、error: ‘chrono_literals’ is not a namespace-name

[ 84%] Building CXX object pgm_map_creator-master/CMakeFiles/collision_map_creator.dir/src/collision_map_creator.cc.o
In file included from /usr/lib/x86_64-linux-gnu/pkgconfig/…/…/…/include/ignition/math6/gz/math/Quaternion.hh:20,
from /usr/lib/x86_64-linux-gnu/pkgconfig/…/…/…/include/ignition/math6/gz/math/Pose3.hh:20,
from /usr/lib/x86_64-linux-gnu/pkgconfig/…/…/…/include/sdformat-9.10/sdf/Actor.hh:23,
from /usr/lib/x86_64-linux-gnu/pkgconfig/…/…/…/include/sdformat-9.10/sdf/sdf.hh:2,
from /home/gly/catkin_ws/src/pgm_map_creator-master/src/request_publisher.cc:4:
/usr/lib/x86_64-linux-gnu/pkgconfig/…/…/…/include/ignition/math6/gz/math/Helpers.hh:40:13: warning: variable templates only available with ‘-std=c++14’ or ‘-std=gnu++14’
40 | constexpr T IGN_MASSMATRIX3_DEFAULT_TOLERANCE = T(10);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/lib/x86_64-linux-gnu/pkgconfig/…/…/…/include/ignition/math6/gz/math/Helpers.hh: In function ‘std::chrono::_V2::steady_clock::duration ignition::math::v6::stringToDuration(const string&)’:
/usr/lib/x86_64-linux-gnu/pkgconfig/…/…/…/include/ignition/math6/gz/math/Helpers.hh:1059:28: error: ‘chrono_literals’ is not a namespace-name
1059 | using namespace std::chrono_literals;
| ^~~~~~~~~~~~~~~
/usr/lib/x86_64-linux-gnu/pkgconfig/…/…/…/include/ignition/math6/gz/math/Helpers.hh: In function ‘std::chrono::_V2::steady_clock::time_point ignition::math::v6::stringToTimePoint(const string&)’:
/usr/lib/x86_64-linux-gnu/pkgconfig/…/…/…/include/ignition/math6/gz/math/Helpers.hh:1101:28: error: ‘chrono_literals’ is not a namespace-name
1101 | using namespace std::chrono_literals;

   报错 4、error: ‘optional’ in namespace ‘std’ does not name a template type

[ 84%] Building CXX object pgm_map_creator-master/CMakeFiles/request_publisher.dir/src/request_publisher.cc.o
In file included from /usr/lib/x86_64-linux-gnu/pkgconfig/…/…/…/include/ignition/math6/gz/math/Box.hh:23,
from /usr/lib/x86_64-linux-gnu/pkgconfig/…/…/…/include/ignition/math6/gz/math.hh:30,
from /usr/lib/x86_64-linux-gnu/pkgconfig/…/…/…/include/sdformat-9.10/sdf/Param.hh:33,
from /usr/lib/x86_64-linux-gnu/pkgconfig/…/…/…/include/sdformat-9.10/sdf/Element.hh:28,
from /usr/lib/x86_64-linux-gnu/pkgconfig/…/…/…/include/sdformat-9.10/sdf/Actor.hh:25,
from /usr/lib/x86_64-linux-gnu/pkgconfig/…/…/…/include/sdformat-9.10/sdf/sdf.hh:2,
from /home/gly/catkin_ws/src/pgm_map_creator-master/src/request_publisher.cc:4:
/usr/lib/x86_64-linux-gnu/pkgconfig/…/…/…/include/ignition/math6/gz/math/Plane.hh:133:20: error: ‘optional’ in namespace ‘std’ does not name a template type
133 | public: std::optional<Vector3> Intersection(
| ^~~~~~~~
/usr/lib/x86_64-linux-gnu/pkgconfig/…/…/…/include/ignition/math6/gz/math/Plane.hh:133:15: note: ‘std::optional’ is only available from C++17 onwards

   第二类错误(报错3和报错4)的报错原因及解决方法:

   报错3和报错4是因为在编译文件中设定了低版本的C++,而又调用了一些高版本C++才有的内容,如报错3中提到的内容需要C++ 14及以上版本,而报错4中提到的内容,需要C++17及以上版本,所以只需要将功能包pgm_map_creator的CMakeLists.txt文件中关于C++版本的设定改为17就可以了

   即,将

set (CMAKE_CXX_FLAGS "-g -Wall -std=c++11")

   改为:

set (CMAKE_CXX_FLAGS "-g -Wall -std=c++17")

   三、第三类错误 Gazebo版更新函数名发生改变导致的错误

   报错 5、error: ‘class gazebo::physics::World’ has no member named ‘GetName’; did you mean ‘Name’?

[ 84%] Building CXX object pgm_map_creator-master/CMakeFiles/collision_map_creator.dir/src/collision_map_creator.cc.o
/home/gly/catkin_ws/src/pgm_map_creator-master/src/collision_map_creator.cc: In member function ‘virtual void gazebo::CollisionMapCreator::Load(gazebo::physics::WorldPtr, sdf::v9::ElementPtr)’:
/home/gly/catkin_ws/src/pgm_map_creator-master/src/collision_map_creator.cc:36:23: error: ‘class gazebo::physics::World’ has no member named ‘GetName’; did you mean ‘Name’?
36 | node->Init(world->GetName());
| ^~~~~~~
| Name
/home/gly/catkin_ws/src/pgm_map_creator-master/src/collision_map_creator.cc: In member function ‘void gazebo::CollisionMapCreator::create(gazebo::CollisionMapRequestPtr&)’:

   报错 6、error: ‘class gazebo::physics::World’ has no member named ‘GetPhysicsEngine’; did you mean ‘SetPhysicsEnabled’?

/home/gly/catkin_ws/src/pgm_map_creator-master/src/collision_map_creator.cc: In member function ‘void gazebo::CollisionMapCreator::create(gazebo::CollisionMapRequestPtr&)’:
/home/gly/catkin_ws/src/pgm_map_creator-master/src/collision_map_creator.cc:92:55: error: ‘class gazebo::physics::World’ has no member named ‘GetPhysicsEngine’; did you mean ‘SetPhysicsEnabled’?
92 | gazebo::physics::PhysicsEnginePtr engine = world->GetPhysicsEngine();
| ^~~~~~~~~~~~~~~~
| SetPhysicsEnabled
make[2]: *** [pgm_map_creator-master/CMakeFiles/collision_map_creator.dir/build.make:63:pgm_map_creator-master/CMakeFiles/collision_map_creator.dir/src/collision_map_creator.cc.o] 错误 1

在这里插入图片描述

   第三类错误(报错5和报错6)的报错原因及解决方法:

   报错原因,就是随着Gazebo版本的更新,一些函数名发生了改变,导致调用时发生了错误,只需要分别将功能包pgm_map_creator的collision_map_creator.cc文件中的调用的函数进行对应的替换即可,如下所示:

对于 GetName()函数,改为使用 Name()。
对于 GetPhysicsEngine()函数,改为使用 Physics()

   四、第四类错误 链接库缺失报错

   报错 7、error: error adding symbols: DSO missing from command line collect2: error: ld returned 1 exit status

[ 84%] Building CXX object pgm_map_creator-master/CMakeFiles/collision_map_creator.dir/src/collision_map_creator.cc.o
/usr/bin/ld: CMakeFiles/request_publisher.dir/src/request_publisher.cc.o: undefined reference to symbol ‘pthread_join@@GLIBC_2.2.5’
/usr/bin/ld: /lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
make[2]: *** [pgm_map_creator-master/CMakeFiles/request_publisher.dir/build.make:87:/home/gly/catkin_ws/devel/lib/pgm_map_creator/request_publisher] 错误 1
make[1]: *** [CMakeFiles/Makefile2:467:pgm_map_creator-master/CMakeFiles/request_publisher.dir/all] 错误 2
make[1]: *** 正在等待未完成的任务…
[ 92%] Linking CXX shared library /home/gly/catkin_ws/devel/lib/libcollision_map_creator.so
[ 92%] Built target collision_map_creator
make: *** [Makefile:141:all] 错误 2
Invoking “make -j2 -l2” failed

在这里插入图片描述

   第四类错误(报错7)的报错原因及解决方法:

   找不到所需的链接库pthread 所产生的报错,为了确保在链接过程中包含 pthread 库,可以在 CMakeLists.txt 中的 target_link_libraries 命令中添加 Threads::Threads 以显式链接 pthread 库。此外,还需要添加 find_package(Threads REQUIRED) 到 CMakeLists.txt 中。

   (1)在以下语句

find_package(Boost REQUIRED COMPONENTS system)

   后面添加find_package(Threads REQUIRED),如下所示:

find_package(Boost REQUIRED COMPONENTS system)
find_package(Threads REQUIRED)

   (2)将以下语句

add_executable (request_publisher src/request_publisher.cc)
target_link_libraries(request_publisher collision_map_creator_msgs ${Boost_LIBRARIES} ${GAZEBO_LIBRARIES} ${SDF_LIBRARIES})
add_dependencies(request_publisher collision_map_creator_msgs)

add_library(collision_map_creator SHARED src/collision_map_creator.cc )
target_link_libraries(collision_map_creator collision_map_creator_msgs ${Boost_LIBRARIES} ${GAZEBO_LIBRARIES} ${SDF_LIBRARIES})
add_dependencies(collision_map_creator collision_map_creator_msgs)

   改为:

add_executable(request_publisher src/request_publisher.cc)
target_link_libraries(request_publisher collision_map_creator_msgs ${Boost_LIBRARIES} ${GAZEBO_LIBRARIES} ${SDF_LIBRARIES} Threads::Threads)  # 添加 Threads::Threads
add_dependencies(request_publisher collision_map_creator_msgs)

add_library(collision_map_creator SHARED src/collision_map_creator.cc)
target_link_libraries(collision_map_creator collision_map_creator_msgs ${Boost_LIBRARIES} ${GAZEBO_LIBRARIES} ${SDF_LIBRARIES} Threads::Threads)  # 添加 Threads::Threads
add_dependencies(collision_map_creator collision_map_creator_msgs)

   经过以上修改,至此,pgm_map_creator功能包就可以在编译成功,并在配ROS Noetic中使用了

在这里插入图片描述

   五、第五类错误 运行报错重复加载 vector2d.proto 文件或找不到vector2d.pb.h

   报错 8、[libprotobuf ERROR google/protobuf/ descriptor_database.cc:58] File already exists in database: vector2d.proto
[libprotobuf FATAL google/protobuf/descriptor.cc:1358] CHECK failed: GeneratedDatabase()->Add(encoded_file_descriptor, size): terminate called after throwing an instance of 'google::protobuf::FatalException’what(): CHECK failed: GeneratedDatabase()->Add(encoded_file_descriptor, size):

process[request_publisher-1]: started with pid [16177]
[libprotobuf ERROR google/protobuf/descriptor_database.cc:58] File already exists in database: vector2d.proto
[libprotobuf FATAL google/protobuf/descriptor.cc:1358] CHECK failed: GeneratedDatabase()->Add(encoded_file_descriptor, size):
terminate called after throwing an instance of ‘google::protobuf::FatalException’
what(): CHECK failed: GeneratedDatabase()->Add(encoded_file_descriptor, size):
[request_publisher-1] process has died [pid 16177, exit code -6, cmd /home/gly/catkin_ws/devel/lib/pgm_map_creator/request_publisher (-15,15)(15,15)(15,-15)(-15,-15) 5 0.01 /home/gly/catkin_ws/src/pgm_map_creator/maps/map __name:=request_publisher __log:=/home/gly/.ros/log/c2d74060-3acb-11ef-a493-fd3fb52fdc63/request_publisher-1.log].
log file: /home/gly/.ros/log/c2d74060-3acb-11ef-a493-fd3fb52fdc63/request_publisher-1*.log
all processes on machine have died, roslaunch will exit
shutting down processing monitor…
… shutting down processing monitor complete
done

   报错 9、fatal error: vector2d.pb.h: 没有那个文件或目录

[ 28%] Building CXX object pgm_map_creator/msgs/CMakeFiles/collision_map_creator_msgs.dir/collision_map_request.pb.cc.o
In file included from /home/gly/catkin_ws/build/pgm_map_creator/msgs/collision_map_request.pb.cc:4:
/home/gly/catkin_ws/build/pgm_map_creator/msgs/collision_map_request.pb.h:33:10: fatal error: vector2d.pb.h: 没有那个文件或目录
33 | #include “vector2d.pb.h”
| ^~~~~~~~~~~~~~~
compilation terminated.
make[2]: *** [pgm_map_creator/msgs/CMakeFiles/collision_map_creator_msgs.dir/build.make:71:pgm_map_creator/msgs/CMakeFiles/collision_map_creator_msgs.dir/collision_map_request.pb.cc.o] 错误 1
make[1]: *** [CMakeFiles/Makefile2:899:pgm_map_creator/msgs/CMakeFiles/collision_map_creator_msgs.dir/all] 错误 2
make: *** [Makefile:141:all] 错误 2

   报错8和报错9是互补的,什么意思呢,当pgm_map_creator功能包的msgs文件夹下的CMakelists.txt文件包含以下三行语句

${PROTOBUF_IMPORT_DIRS}/vector2d.proto
${PROTOBUF_IMPORT_DIRS}/header.proto
${PROTOBUF_IMPORT_DIRS}/time.proto

   即,完整的msgs文件夹下的CMakelists.txt文件内容如下所示时,编译可以正常通过,但是运行时会遇到报错8,无法使用

find_package(Protobuf REQUIRED)

set(PROTOBUF_IMPORT_DIRS)
foreach(ITR ${GAZEBO_INCLUDE_DIRS})
  if(ITR MATCHES ".*gazebo-[0-9.]+$")
    set(PROTOBUF_IMPORT_DIRS "${ITR}/gazebo/msgs/proto")
  endif()
endforeach()

set (msgs
  collision_map_request.proto
  ${PROTOBUF_IMPORT_DIRS}/vector2d.proto
  ${PROTOBUF_IMPORT_DIRS}/header.proto
  ${PROTOBUF_IMPORT_DIRS}/time.proto
)

PROTOBUF_GENERATE_CPP(PROTO_SRCS PROTO_HDRS ${msgs})
add_library(collision_map_creator_msgs SHARED ${PROTO_SRCS})
target_include_directories(collision_map_creator_msgs PUBLIC ${PROTOBUF_INCLUDE_DIRS})
target_link_libraries(collision_map_creator_msgs ${PROTOBUF_LIBRARY})

   当,不包含该三条语句,将其注释掉,即,完整的msgs文件夹下的CMakelists.txt文件内容如下所示时,编译无法正常通过,编译时会遇到报错9,无法使用

find_package(Protobuf REQUIRED)

set(PROTOBUF_IMPORT_DIRS)
foreach(ITR ${GAZEBO_INCLUDE_DIRS})
  if(ITR MATCHES ".*gazebo-[0-9.]+$")
    set(PROTOBUF_IMPORT_DIRS "${ITR}/gazebo/msgs/proto")
  endif()
endforeach()

set (msgs
  collision_map_request.proto
  #${PROTOBUF_IMPORT_DIRS}/vector2d.proto
  #${PROTOBUF_IMPORT_DIRS}/header.proto
  #${PROTOBUF_IMPORT_DIRS}/time.proto
)

PROTOBUF_GENERATE_CPP(PROTO_SRCS PROTO_HDRS ${msgs})
add_library(collision_map_creator_msgs SHARED ${PROTO_SRCS})
target_include_directories(collision_map_creator_msgs PUBLIC ${PROTOBUF_INCLUDE_DIRS})
target_link_libraries(collision_map_creator_msgs ${PROTOBUF_LIBRARY})

   看起来,好像报错8和9必然会遇到一个,必然无法使用,那么如何解决?,历经2天,机缘巧合之下,我莫名其妙的找到了解决方法,就是先包含这三条语句,编译一遍,让他编译成功,此时运行时会遇到报错8,然后再注释掉这三行语句,再编译一遍,此时,发现就可以编译通过了,而且运行时,也不会报错了,可以正常使用。


  

   1、查看Boost版本的指令

dpkg -s libboost-dev | grep 'Version'

在这里插入图片描述

   2、查看Gazebo版本的指令

dpkg -l | grep gazebo

在这里插入图片描述

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

慕羽★

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值