当前位置: 首页 > news >正文

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
在这里插入图片描述

#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/**"

在这里插入图片描述

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

在这里插入图片描述


⭐⭐⭐嘟嘟崽 ⭐⭐⭐
⭐⭐⭐祝你成功 ⭐⭐⭐
http://www.lryc.cn/news/56204.html

相关文章:

  • 一位年薪35W的测试被开除,回怼的一番话,令人沉思
  • 【Docker】Docker常用命令
  • 【linux基础】7.linux系统自定义应用名和应用图标
  • 10.网络爬虫—MongoDB详讲与实战
  • C4D -> Three.js资产制作与导入流程
  • 【博学谷学习记录】大数据课程-学习十三周总结
  • Spring Cloud快速入门
  • 论文学习——VideoGPT
  • Flutter系列(五)底部导航详解
  • 『pyqt5 从0基础开始项目实战』02. 页面布局设计(保姆级图文)
  • 【Python机器学习】——平均中位数模式
  • Windows窗口
  • Spring Transaction 源码解读
  • [Netty] Channel和ChannelFuture和ChannelFutureListener (六)
  • 条件渲染
  • springboot(10)异步任务
  • 清华大学开源的chatGLM-6B部署实战
  • 通过矩阵从整体角度搞懂快速傅里叶变换原理
  • 【C++从0到1】25、C++中嵌套使用循环
  • FastDFS与Nginx结合搭建文件服务器,并内网穿透实现公网访问
  • 密集场景下的行人跟踪替代算法,头部跟踪算法 | CVPR 2021
  • Matlab与ROS(1/2)---服务端和客户端数据通信(五)
  • 数字化转型的避坑指南:细说数字化转型十二大坑
  • pt05Encapsulationinherit
  • 面向对象编程(基础)9:封装性(encapsulation)
  • fate-serving-server增加取数逻辑并源码编译
  • 循环队列、双端队列 C和C++
  • 正则表达式(语法+例子)
  • Properties和IO流集合的方法
  • python 生成器、迭代器、动态新增属性及方法