当前位置:网站首页>【ROS进阶篇】第二讲 自定义头、源文件封装
【ROS进阶篇】第二讲 自定义头、源文件封装
2022-06-29 20:58:00 【生如昭诩】
【ROS进阶篇】第二讲 自定义头、源文件封装

文章目录
前言
在入门篇的课程中我们对于代码的封装并没有做多研究,更多的是编写cpp文件后就编译使用,而想要实现工程化的进行ROS系统使用,就需要使用头文件与源文件的方式封装代码,而对于不同的封装方式来说,编译规则的处理也不尽相同,需进一步分析。

自定义头文件封装
1. 流程
- 概念:自行设计头文件和可执行文件,并将可执行文件本身作为源文件
- 流程:
- 编写头文件
- 编写可执行文件/作为源文件
- 修改编译规则配置文件编译
2. 头文件
- 代码演示:
#ifndef _HELLO_H
#define _HELLO_H
namespace hello_ns{
class HelloPub {
public:
void run();
};
}
#endif
- 简要分析:
- 宏定义与预处理:防止头文件的重复包含和编译
#ifndef _HELLO_H //先测试x是否被宏定义过 #define _HELLO_H //程序段1 //如果x没有被宏定义过,定义x,并编译程序段 1 ... #endif //终止if
- 命名空间:防止出现标识符之间的冲突,区分类似的函数、类、变量
namespace hello_ns{ ... }
- c_cpp_properties.json:修改includepath属性防止抛出异常;
"/home/用户/工作空间/src/功能包/include/**"
3. 可执行文件
- 代码展示:
#include "ros/ros.h"
#include "test_head/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 helloPub;
helloPub.run();
return 0;
}
4. 修改编译规则
- 添加include:
include_directories(
include
${catkin_INCLUDE_DIRS}
)
- 配置文件依赖项与链接:
add_executable(hello src/hello.cpp)
add_dependencies(hello ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(hello
${catkin_LIBRARIES}
)
自定义源文件封装
1. 流程
- 概念:分别定义源文件、头文件、可执行文件,在可执行文件中包含头文件;
- 流程:
- 编写头文件;
- 编写源文件;
- 编写可执行文件;
- 编写配置文件并执行;
2. 头文件
- 代码展示:head.h
#ifndef _HEAD_H
#define _HEAD_H
namespace hello_ns {
class My {
public:
void run();
};
}
#endif
- 文件目录:功能包下的include/功能包名下(同样需要配置includepath)
3. 源文件
- 代码展示:source.cpp
#include "test_head_src/head.h"
#include "ros/ros.h"
namespace hello_ns{
void My::run(){
ROS_INFO("hello,head and src ...");
}
}
4. 可执行文件
- 代码展示:use_head.cpp
#include "ros/ros.h"
#include "test_head_src/head.h"
int main(int argc, char *argv[])
{
ros::init(argc,argv,"hahah");
hello_ns::My my;
my.run();
return 0;
}
5. 修改编译规则
- 头文件与源文件:
- include项:
include_directories( include ${catkin_INCLUDE_DIRS} )
- 声明C++库
add_library(head include/test_head_src/head.h src/source.cpp )
- 依赖项与链接
add_dependencies(head >${ ${ PROJECT_NAME}_EXPORTED_TARGETS} >${ catkin_EXPORTED_TARGETS}) target_link_libraries(head ${ catkin_LIBRARIES} )
- 可执行文件:
依赖项与链接:
add_executable(use_head src/use_head.cpp) add_dependencies(use_head >${${PROJECT_NAME}_EXPORTED_TARGETS} >${catkin_EXPORTED_TARGETS}) #此处需要添加之前设置的 head 库 target_link_libraries(use_head head ${catkin_LIBRARIES} )
总结
- 声明:本节博客部分参考了CSDN用户赵虚左的ROS教程,下篇博客将会主要聚焦于ROS的运行管理上,介绍元功能包与工作空间覆盖的概念。

边栏推荐
- Practical guide to GStreamer application development (V)
- 【云原生实战】KubeSphere实战——多租户系统实战
- About Effect Size
- 知识蒸馏(Knowledge Distilling)学习笔记
- 项目中退出操作
- leetcode:370. 区间加法
- How can colleges and universities build future oriented smart campus based on cloud native? Full stack cloud native vs traditional technology architecture
- What problems should be avoided when using the points mall games for marketing?
- [today in history] June 29: SGI and MIPS merged; Microsoft acquires PowerPoint developer; News corporation sells MySpace
- leetcode:238. Product of arrays other than itself
猜你喜欢

PostgreSQL每周新聞—6月22日

At least 3 years for learning amplifier?

LSF-bsub命令

High energy live broadcast, a gathering of celebrities! We invite you to explore bizdevops.

导航【微机原理】

Mysql Json 数据类型&函数

Design of VHDL telephone billing system

"Operation and maintenance department has Xiao Deng" to review and analyze file and folder access rights

leetcode:307. 区域和检索 - 数组可修改
![Navigation experiment [microcomputer principle] [experiment]](/img/79/8311a409113331e72f650a83351b46.png)
Navigation experiment [microcomputer principle] [experiment]
随机推荐
Mapbox GL development tutorial (12): loading surface layer data
企业实施MES系统的关键点详解
【云原生实战】KubeSphere实战——多租户系统实战
每周招聘|DBA数据工程师,年薪35+ ,梦起九州,星河灿烂!
I found another cross domain method by chance. I don't know if anyone has ever played this way
「运维有小邓」日志分析工具使用越来越频繁的原因
Leading by 11%, Huawei cloud sky chip AI solver once again topped the international authoritative list
What are the mainstream brands of smart door locks? What characteristics should we pay attention to when purchasing door locks?
期末复习【微机原理】
Jupyter service installation and startup
flutter BottomNavigationBar切换页面保持状态
输入年份与月份,求该月共有多少天
分析影响导电滑环传输信号的因素
Analysis of the factors affecting the transmission signal of the conductive slip ring
Basic qualities of management personnel
Deploy web using gunicorn Py application
Topic39——78. subset
LSF bsub command
. NETCORE unified authentication authorization learning - run (1)
MySQL JSON data types & functions