ROS_hydro_helloworld.cpp_g++编译

本文介绍了一个简单的ROS (Robot Operating System) Hello World程序实现步骤。包括使用C++创建节点、编译源代码、运行程序以及解决常见错误的过程。

1.创建文件

mkdir hello_world_tutorial
cd hello_world_tutorial

hello_world_node.cpp

// Include the ROS C++ APIs
#include <ros/ros.h>

// Standard C++ entry point
int main(int argc, char** argv) {
  // Announce this program to the ROS master as a "node" called "hello_world_node"
  ros::init(argc, argv, "hello_world_node");
  // Start the node resource managers (communication, time, etc)
  ros::start();
  // Broadcast a simple log message
  ROS_INFO_STREAM("Hello, world!");
  // Process ROS callbacks until receiving a SIGINT (ctrl-c)
  ros::spin();
  // Stop the node's resources
  ros::shutdown();
  // Exit tranquilly
  return 0;
}

2.编译

g++ hello_world_node.cpp -o hello_world_node -I/opt/ros/hydro/include -L/opt/ros/hydro/lib -Wl,-rpath,/opt/ros/hydro/lib -lroscpp -lrosconsole -lrostime

  • hello_world_node.cpp The source file(s) to compile
  • -o hello_world_node The name of the output file (the executable, in this case)
  • -I/opt/ros/hydro/include An instruction to look for C++ header files in /opt/ros/hydro/include
  • -L/opt/ros/hydro/lib An instruction to look for static libraries in /opt/ros/hydro/lib
  • -Wl,-rpath,/opt/ros/hydro/lib An instruction to look for shared libraries in /opt/ros/hydro/lib
  • -lroscpp Link against the library libroscpp.so (ROS C++ bindings)
  • -lrosconsole Link against the library librosconsole.so (ROS distributed logging)
  • -lrostime Link against the library librostime.so (ROS time measurement)

3.运行

./hello_world_node

4.常见错误及解决

4.1 ERROR: ROS_MASTER_URI is not defined (Bad Environment)

[FATAL] [1392021564.231775029]: ROS_MASTER_URI is not defined in the
environment. Either type the following or (preferrably) add this to your
~/.bashrc file in order set up your local machine as a ROS master:

export ROS_MASTER_URI=http://localhost:11311

then, type 'roscore' in another shell to actually launch the master program.
解决:
source /opt/ros/hydro/setup.sh
./hello_world_node

ERROR: Failed to contact master (Good Environment, but no ROS Master)

[ERROR] [1392014787.460431497]: [registerPublisher] Failed to contact master at [localhost:11311].  Retrying...
解决:
roscore

参考:http://www.jbohren.com/articles/roscpp-hello-world/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值