c++的程序在ros下怎么运行

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c++的程序在ros下怎么运行相关的知识,希望对你有一定的参考价值。

参考技术A 1 先建立工作空间~/catkin_ws
2 在工作空间~/catkin_ws下创建包pkg目录second
好处是通过创建包自动生成package.xml和CMakeLists.txt
3 在second目录下创建hello.cpp源代码文件
~~~~~~~~~~~~~~~~~~~~~~~~~~~
eagle:~/catkin_ws$ cd src
eagle:~/catkin_ws/src$ tree second/
second/
├── CMakeLists.txt
├── package.xml
└── src
└── hello.cpp
1 directory, 3 files

4 编写hello.cpp文件
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <ros/ros.h>
int main(int argc, char **argv)
ros::init(argc, argv, "helloros1234444");
ros::NodeHandle n;
ROS_INFO("Hello, ROS!");
ros::spinOnce();

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

5 修改编译makefile文件 CMakeLists.txt
~~~~~~~~~~~~~~~~~~
cmake_minimum_required(VERSION 2.8.3)
project(simple_examples)
find_package(catkin REQUIRED COMPONENTS roscpp)
catkin_package()
include_directories(include $catkin_INCLUDE_DIRS)
add_executable(hello hello.cpp)
target_link_libraries(hello $catkin_LIBRARIES)
~~~~~~~~~~~~~~~~~~

6 回到~/catkin_ws编译程序
liao@liao-eagle:~/catkin_ws$ catkin_make
Base path: /home/liao/catkin_ws
Source space: /home/liao/catkin_ws/src
Build space: /home/liao/catkin_ws/build
Devel space: /home/liao/catkin_ws/devel
Install space: /home/liao/catkin_ws/install
####
#### Running command: "make cmake_check_build_system" in "/home/liao/catkin_ws/build"
####
####
#### Running command: "make -j2 -l2" in "/home/liao/catkin_ws/build"
####
Scanning dependencies of target hello
[100%] Building CXX object second/CMakeFiles/hello.dir/src/hello.cpp.o
Linking CXX executable /home/liao/catkin_ws/devel/lib/second/hello
[100%] Built target hello
liao@liao-eagle:~/catkin_ws$

7 测试c++程序
7.1一个终端里运行roscore
7.2 另外一个终端里运行 rosrun second hello
liao@liao-eagle:~/catkin_ws$ rosrun second hello
[ INFO] [1442208641.400826666]: Hello, ROS!
liao@liao-eagle:~/catkin_ws$ rospack find second
/home/liao/catkin_ws/src/second
liao@liao-eagle:~/catkin_ws$
望采纳

以上是关于c++的程序在ros下怎么运行的主要内容,如果未能解决你的问题,请参考以下文章

蓝桥ROS机器人之C++基础1总结和测评

ORBSLAM2在ROS下运行

pl-svo在ROS下运行笔记

蓝桥ROS机器人之C++系列奇妙甜甜圈

C++文件与STM32进行串口通信例程

简述在VC++6.0环境下运行一个C++源程序的步骤