Gazebo11 与 Protobuf 版本管理
Gazebo11 与 Protobuf 版本管理
编译双目相机gazebo插件出现的问题
https://github.com/nilseuropa/realsense_ros_gazebo/tree/master
问题说明
- 编译版本提示问题: --proto版本问题
/usr/include/gazebo-11/gazebo/msgs/poses_stamped.pb.h:12:2: error: #error This file was generated by a newer version of protoc which is12 | #error This file was generated by a newer version of protoc which is| ^~~~~
/usr/include/gazebo-11/gazebo/msgs/poses_stamped.pb.h:13:2: error: #error incompatible with your Protocol Buffer headers. Please update13 | #error incompatible with your Protocol Buffer headers. Please update| ^~~~~
/usr/include/gazebo-11/gazebo/msgs/poses_stamped.pb.h:14:2: error: #error your headers.14 | #error your headers.
对应文件检查版本: vim /usr/include/gazebo-11/gazebo/msgs/poses_stamped.pb.h
#if GOOGLE_PROTOBUF_VERSION < 3006001
#error This file was generated by a newer version of protoc which is
#error incompatible with your Protocol Buffer headers. Please update
#error your headers.
#endif
#if 3006001 < GOOGLE_PROTOBUF_MIN_PROTOC_VERSION
#error This file was generated by an older version of protoc which is
#error incompatible with your Protocol Buffer headers. Please
#error regenerate this file with a newer version of protoc.
#endif
- 变量未定义相关问题: --gazebo版本问题
error: ‘ConstPosePtr’ has not been declared
[ 66%] Building CXX object realsense_ros_gazebo/CMakeFiles/realsense_gazebo_plugin.dir/src/gazebo_ros_realsense.cpp.o/usr/include/gazebo-11/gazebo/physics/Entity.hh:268:31: error: ‘ConstPosePtr’ has not been declared268 | private: void OnPoseMsg(ConstPosePtr &_msg);| ^~~~~~~~~~~~
In file included from /usr/include/gazebo-11/gazebo/physics/AdiabaticAtmosphere.hh:23,from /usr/include/gazebo-11/gazebo/physics/physics.hh:3,from /root/test_ws/src/realsense_ros_gazebo/include/realsense_gazebo_plugin/RealSensePlugin.h:23,from /root/test_ws/src/realsense_ros_gazebo/src/RealSensePlugin.cpp:17:
/usr/include/gazebo-11/gazebo/physics/Atmosphere.hh:68:41: error: ‘ConstRequestPtr’ has not been declared68 | protected: virtual void OnRequest(ConstRequestPtr &_msg);
1. 查找本机的 Protobuf (protoc) 版本
在 Gazebo 及其依赖的 protobuf
相关调试中,首先需要确认本机的 protoc
版本。
# which -a protoc
/usr/bin/protoc
/bin/protoc如果 which -a protoc 显示多个 protoc,可以 分别查看不同路径下的版本
/usr/bin/protoc --version
/bin/protoc --version
🔹 方法 1:使用命令行查看 protoc
版本
protoc --version
示例输出:
libprotoc 3.6.1
🔹 方法 2:查找系统安装的 Protobuf 版本
dpkg -l | grep protobuf
示例输出:
ii libprotobuf-dev:amd64 3.6.1-2 amd64 protocol buffers C++ library (development files)
ii libprotobuf-lite23:amd64 3.6.1-2 amd64 protocol buffers C++ library (lite version)
ii protobuf-compiler 3.6.1-2 amd64 compiler for protocol buffer definition files
2. 多 Protobuf 版本管理:删除不匹配的版本
如果系统中存在多个版本的 protoc
,可能导致 Gazebo 及其插件的编译错误。
🔹 方法 1:查找所有 protoc
可执行文件
which -a protoc
示例输出:
/usr/bin/protoc
/usr/local/bin/protoc
🔹 方法 2:检查 protoc
版本冲突
ls -l /usr/bin/protoc
ls -l /usr/local/bin/protoc
如果 /usr/local/bin/protoc
版本不匹配,可以手动删除旧版本:
sudo rm /usr/local/bin/protoc
然后重新安装正确的版本:
sudo apt install protobuf-compiler libprotobuf-dev
检查并修复 protobuf 安装路径
依据查找到的protoc版本,删除执行文件,职能保障protoc --version达到预期的版本。但是在编译文件时读到的宏文件GOOGLE_PROTOBUF_VERSION还是可能从版本里的头文件和库生成的,需要重新删除干净:
(做好备份)
sudo rm -rf /usr/local/include/google/protobuf
sudo rm -rf /usr/local/lib/libprotobuf*
当前运行的程序输出的是 GOOGLE_PROTOBUF_VERSION: 3004000,即 protobuf 版本为 3.4.0,而你系统中安装的 protoc 是 3.6.1。这说明,虽然你在命令行中运行的 protoc --version 显示的是 3.6.1,但你的代码编译时链接的是 protobuf 3.4.0 的库和头文件。
查看 protobuf 头文件路径,检查是否指向了正确的版本:
- 查看 protobuf 头文件路径,检查是否指向了正确的版本:
g++ -Xpreprocessor -v -E check_protobuf_version.cpp
这个命令会输出包含 头文件路径 的详细信息,检查是否使用了 /usr/include/protobuf(或者其他路径)下的头文件。如果有其他路径,可能会导致你链接到错误版本的 protobuf。
2.检查链接库路径,确保链接的是正确版本的 libprotobuf 库:
g++ -v check_protobuf_version.cpp -lprotobuf
查看是否链接的是 /usr/lib/x86_64-linux-gnu/libprotobuf.so 或者其他路径,确保它指向的是 protobuf 3.6.1
- 使用 pkg-config 确保正确的 protobuf 版本
pkg-config 可以帮助你获取正确的编译和链接选项,确保你链接到正确版本的 protobuf。
pkg-config --cflags --libs protobuf
这会输出编译时应使用的标志,确保它指向了 正确的路径。
3. 编译时读取的 protoc
版本 (GOOGLE_PROTOBUF_VERSION
)
Gazebo 在编译时可能使用错误的 protoc
版本。
🔹 方法 1:在 C++ 代码中打印 GOOGLE_PROTOBUF_VERSION
创建 check_protobuf_version.cpp
文件:
#include <iostream>
#include <google/protobuf/stubs/common.h>int main() {std::cout << "GOOGLE_PROTOBUF_VERSION: " << GOOGLE_PROTOBUF_VERSION << std::endl;return 0;
}
编译运行:
g++ check_protobuf_version.cpp -o check_version -lprotobuf
./check_version
示例输出:
GOOGLE_PROTOBUF_VERSION: 3006001
4. 重新生成 Protobuf 头文件
Gazebo 使用 .proto
文件来定义消息格式,这些文件需要正确生成 .pb.h
头文件。
🔹 方法 1:删除旧的 .pb.h
和 .pb.cc
文件
cd /usr/include/gazebo-11/gazebo/msgs
sudo rm -rf *.pb.h *.pb.cc
🔹 方法 2:使用 protoc
重新生成
protoc --cpp_out=. *.proto
🔹 方法 3:检查 msgs
头文件是否正确
ls /usr/include/gazebo-11/gazebo/msgs | grep ".pb.h"
如果某些 .pb.h
缺失,需要手动重新生成。
5. 检查 Gazebo 版本
在调试 Gazebo 相关问题时,需要确认使用的 Gazebo 版本。
🔹 方法 1:查看 Gazebo 版本
gazebo --version
示例输出:
Gazebo multi-robot simulator, version 11.15.1
🔹 方法 2:检查安装的 Gazebo 软件包
dpkg -l | grep gazebo
示例输出:
ii gazebo11 11.15.1 amd64 Open source robotics simulator
6. 解决 Gazebo 物理引擎 (physics/
) 头文件问题
Gazebo 的 physics/
目录存放物理引擎 API,升级 Gazebo 可能导致头文件变化。
🔹 方法 1:确认 physics/
头文件路径
ls /usr/include/gazebo-11/gazebo/physics/
🔹 方法 2:检查 physics.hh
来自哪个软件包
dpkg -S /usr/include/gazebo-11/gazebo/physics/physics.hh
示例输出:
libgazebo11-dev: /usr/include/gazebo-11/gazebo/physics/physics.hh
🔹 方法 3:重新安装 Gazebo
sudo apt remove --purge gazebo11 libgazebo11-dev
sudo apt autoremove
sudo apt update
sudo apt install gazebo11 libgazebo11-dev
7. Gazebo 11.11 vs. 11.15 版本差异
error: ‘ConstPosePtr’ has not been declared
再解决proto版本问题后,重新安装Gazebo, 问题解决,编译成功。
查看新的gazebo版本为11.15
📌 总结
问题 | 解决方案 |
---|---|
查找 protoc 版本 | protoc --version & `dpkg -l |
删除多余 protoc 版本 | which -a protoc & sudo rm /usr/local/bin/protoc |
检查编译时的 protoc 版本 | 编译 check_protobuf_version.cpp |
Gazebo msgs 头文件缺失 | protoc --cpp_out=. *.proto 重新生成 .pb.h |
检查 Gazebo 版本 | gazebo --version & `dpkg -l |
Gazebo physics/ 头文件问题 | dpkg -S physics.hh & 重新安装 libgazebo11-dev |