安装好ROS后,默认已经安装了catkin,接着执行以下步骤
用catkin创建ROS包
$ cd ~/catkin_ws/src
# catkin_create_pkg 创建包的命令
# hello 包的名字
# std_msgs、roscpp 依赖库,如果不清楚需要依赖什么,先把这2个写上,后续再补
$ catkin_create_pkg hello std_msgs roscpp
$ cd ~/catkin_ws
$ catkin_make
$ . ~/catkin_ws/devel/setup.bash
至此,包已经创建完了,会在目录 home/catkin_ws/src/ 下看到名为hello的包。
用catkin编译
在hello包下的目录src中,创建hello_node.cpp,add.cpp文件,在目录include下的目录hello中创建add.h文件
即包hello的目录结构为:
add.h
#ifndef MY_ADD
#define MY_ADD
int Add(int a, int b);
#endif
add.cpp
#include "../include/hello/add.h"
int Add(int a, int b)
{
return a + b;
}
hello_node.cpp
#include "iostream"
#include "../include/hello/add.h"
using namespace std;
int main()
{
cout << Add(1, 2) << endl;
return 0;
}
CMakelist.txt
cmake_minimum_required(VERSION 2.8.3)
project(hello)
find_package(catkin REQUIRED COMPONENTS roscpp std_msgs)
include_directories(
include
${catkin_INCLUDE_DIRS}
)
add_executable(${PROJECT_NAME}_node src/hello_node.cpp src/add.cpp)
target_link_libraries(${PROJECT_NAME}_node
${catkin_LIBRARIES}
)
编译。一直退到目录 catkin_ws 下,执行:
catkin_make
编译成功后,继续执行:
rosrun hello hello_node
会看到输出 3
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)