> 文章列表 > ROS实践11 自定义头文件并调用

ROS实践11 自定义头文件并调用

ROS实践11 自定义头文件并调用

文章目录

    • 运行环境:
    • 思路:
    • 1.1 编写头文件
    • 1.2 includepath添加头文件路径
    • 1.3 编写可执行文件
    • 1.4 配置文件
    • 1.5 编译运行

运行环境:

ubuntu20.04 noetic
宏基暗影骑士笔记本

思路:

类和函数:
头文件 声明
可执行文件 定义调用

另外:在CMakeLists.txt 添加头文件路径 include_directories

1.1 编写头文件

1)在功能包下的 include/功能包名 目录下新建头文件 hello.h
ROS实践11 自定义头文件并调用

#ifndef _HELLO_H
#define _HELLO_Hnamespace hello_ns{class HelloPub {public:void run();
};}#endif

1.2 includepath添加头文件路径

添加到功能包的include文件夹

"/home/duduzai/demo01_ws/src/demo01_pub/include/"

ROS实践11 自定义头文件并调用

1.3 编写可执行文件

在 src 目录下新建文件 hello.cpp
#include " 功能包/.h "

#include "ros/ros.h"
#include "demo01_pub/hello.h"// 命名空间可以更好的管理代码命名,防止命名冲突
namespace hello_ns {void HelloPub::run(){ROS_INFO("自定义头文件的使用....");
}}int main(int argc, char *argv[])
{setlocale(LC_ALL,"");ros::init(argc,argv,"test_head_node");//  创建 hello_ns命名空间 HelloPub类 对象helloPubhello_ns::HelloPub helloPub;// 通过对象.调用run函数helloPub.run();return 0;
}

1.4 配置文件

配置CMakeLists.txt文件

include_directories(
include${catkin_INCLUDE_DIRS}
)

可执行文件配置和以前一样

add_executable(hello src/hello.cpp)target_link_libraries(hello${catkin_LIBRARIES}
)

1.5 编译运行

# 编译
ctrl+shift+B# 运行
roscore
source ./devel/setup.bash
rosrun demo01_pub hello.cpp

ROS实践11 自定义头文件并调用


⭐⭐⭐嘟嘟崽 ⭐⭐⭐ ⭐⭐⭐祝你成功 ⭐⭐⭐