fish_protocol在ros2 humble下运行的问题
-
编译example_fish_protocol时出现如下警告,该如何修改?
Starting >>> example_fish_protocol
--- stderr: example_fish_protocol
In file included from /usr/include/boost/smart_ptr/detail/sp_thread_sleep.hpp:22,
from /usr/include/boost/smart_ptr/detail/yield_k.hpp:23,
from /usr/include/boost/smart_ptr/detail/spinlock_gcc_atomic.hpp:14,
from /usr/include/boost/smart_ptr/detail/spinlock.hpp:42,
from /usr/include/boost/smart_ptr/detail/spinlock_pool.hpp:25,
from /usr/include/boost/smart_ptr/shared_ptr.hpp:29,
from /usr/include/boost/shared_ptr.hpp:17,
from /usr/include/boost/date_time/time_clock.hpp:17,
from /usr/include/boost/date_time/posix_time/posix_time_types.hpp:10,
from /usr/include/boost/asio/time_traits.hpp:23,
from /usr/include/boost/asio/detail/timer_queue_ptime.hpp:22,
from /usr/include/boost/asio/detail/deadline_timer_service.hpp:29,
from /usr/include/boost/asio/basic_deadline_timer.hpp:25,
from /usr/include/boost/asio.hpp:25,
from /usr/local/include/fish_protocol/fish_protocol.h:10,
from /home/hhx/ros2_protocol/fish_protocol/examples/example_ros2/example_fish_protocol/src/example_fish_protocol.cpp:1:
/usr/include/boost/bind.hpp:36:1: note: ‘#pragma message: The practice of declaring the Bind placeholders (_1, _2, ...) in the global namespace is deprecated. Please use <boost/bind/bind.hpp> + using namespace boost::placeholders, or define BOOST_BIND_GLOBAL_PLACEHOLDERS to retain the current behavior.’
36 | BOOST_PRAGMA_MESSAGE(
| ^~~~~~~~~~~~~~~~~~~~Finished <<< example_fish_protocol [3.78s]
Summary: 1 package finished [3.92s]
1 package had stderr output: example_fish_protocol
![替代文字] -
这个警告信息表明你的代码(或者依赖的库)在使用 Boost 的
bind
功能时,使用了全局命名空间的占位符(如_1
,_2
, ...),这种做法已经被弃用。Boost 推荐使用新的方式来引用这些占位符,以避免潜在的命名冲突。警告信息中提供了一个解决方案:
-
使用
<boost/bind/bind.hpp>
并使用boost::placeholders
命名空间:- 修改你的代码,包含头文件
<boost/bind/bind.hpp>
而不是<boost/bind.hpp>
。 - 在需要使用占位符的地方,添加
using namespace boost::placeholders;
。
- 修改你的代码,包含头文件
-
定义
BOOST_BIND_GLOBAL_PLACEHOLDERS
:- 如果你不想修改现有的代码,可以在编译时定义宏
BOOST_BIND_GLOBAL_PLACEHOLDERS
来保持当前的行为。这可以通过在编译命令中添加-DBOOST_BIND_GLOBAL_PLACEHOLDERS
来实现。
- 如果你不想修改现有的代码,可以在编译时定义宏
针对你的 ROS 2 项目,你可以按照以下步骤操作:
方法 1:修改代码
- 修改包含的头文件:
在你的源文件中(如example_fish_protocol.cpp
),将#include <boost/bind.hpp>
替换为#include <boost/bind/bind.hpp>
。 - 添加命名空间:
在文件顶部或在使用占位符之前,添加using namespace boost::placeholders;
。
方法 2:定义宏
- 修改 CMakeLists.txt:
在 CMakeLists.txt 文件中,找到ament_add_executable
或类似的命令,添加编译选项:ament_add_executable(example_fish_protocol src/example_fish_protocol.cpp) ament_target_environments(example_fish_protocol AMENT_CURRENT_PREFIX) target_compile_definitions(example_fish_protocol PRIVATE BOOST_BIND_GLOBAL_PLACEHOLDERS)
选择其中一种方法来解决警告问题。如果你控制源代码(包括依赖的第三方库),推荐使用方法 1,因为它更符合现代的 C++ 和 Boost 库的使用习惯。如果你只是用户,且不希望或不能修改源代码,方法 2 可能更合适。不过,长期来看,更新代码以符合新的库标准是更好的做法。
以上为生成回答,仅供参考~
-