当前位置:网站首页>[advanced ROS chapter] lesson 2 custom header and source file encapsulation
[advanced ROS chapter] lesson 2 custom header and source file encapsulation
2022-06-29 21:00:00 【Life is like Zhaoxu】
【ROS Advanced 】 The second is Custom header 、 Source file encapsulation

List of articles
Preface
In the introductory course, we did not do much research on code encapsulation , More about writing cpp File is compiled and used , And want to achieve engineering In the process of ROS System use , You need to use Header file and source file are encapsulated Code , For different packaging methods , Compilation Rules The treatment is not the same , Further analysis is needed .

Custom header file encapsulation
1. technological process
- Concept : Design your own header file and executable file , And will Executable file In itself As a source file
- technological process :
- Write a header file
- Write executable / As a source file
- Modify the compilation rule configuration file to compile
2. The header file
- Code demonstration :
#ifndef _HELLO_H
#define _HELLO_H
namespace hello_ns{
class HelloPub {
public:
void run();
};
}
#endif
- Brief analysis :
- Macro definition and preprocessing : Prevent repeated inclusion and compilation of header files
#ifndef _HELLO_H // Test first x Whether defined by macro #define _HELLO_H // Procedures section 1 // If x Not defined by macro , Definition x, And compile the program segment 1 ... #endif // End if
- Namespace : Prevent conflicts between identifiers , Distinguish between similar functions 、 class 、 Variable
namespace hello_ns{ ... }
- c_cpp_properties.json: modify includepath Property to prevent throwing exceptions ;
"/home/ user / working space /src/ Function pack /include/**"
3. Executable file
- Code display :
#include "ros/ros.h"
#include "test_head/hello.h"
namespace hello_ns {
void HelloPub::run(){
ROS_INFO(" Use of custom header file ....");
}
}
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. Modify Compilation Rules
- add to include:
include_directories(
include
${catkin_INCLUDE_DIRS}
)
- Configuration file dependencies and links :
add_executable(hello src/hello.cpp)
add_dependencies(hello ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(hello
${catkin_LIBRARIES}
)
Custom source file encapsulation
1. technological process
- Concept : Define source files separately 、 The header file 、 Executable file , Include header files in the executable ;
- technological process :
- Write a header file ;
- Write source file ;
- Write executable ;
- Write the configuration file and execute ;
2. The header file
- Code display :head.h
#ifndef _HEAD_H
#define _HEAD_H
namespace hello_ns {
class My {
public:
void run();
};
}
#endif
- File directory : Under the function package include/ Under the function package name ( You also need to configure includepath)
3. Source file
- Code display :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. Executable file
- Code display :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. Modify Compilation Rules
- Header file and source file :
- include term :
include_directories( include ${catkin_INCLUDE_DIRS} )
- Statement C++ library
add_library(head include/test_head_src/head.h src/source.cpp )
- Dependencies and links
add_dependencies(head >${ ${ PROJECT_NAME}_EXPORTED_TARGETS} >${ catkin_EXPORTED_TARGETS}) target_link_libraries(head ${ catkin_LIBRARIES} )
- Executable file :
Dependencies and links :
add_executable(use_head src/use_head.cpp) add_dependencies(use_head >${${PROJECT_NAME}_EXPORTED_TARGETS} >${catkin_EXPORTED_TARGETS}) # Here you need to add the previously set head library target_link_libraries(use_head head ${catkin_LIBRARIES} )
summary
- Statement : The blog section of this section refers to CSDN User zhaoxuzuo ROS course , The next blog will focus on ROS On the operation management of , Introduce the concept of meta function pack and workspace coverage .

边栏推荐
- 「运维有小邓」Active Directory 密码过期通知功能
- Design of VHDL telephone billing system
- STL教程6-deque、stack、queue、list容器
- Cantata version 9.5 has officially passed the sgs-t Ü V certification and conforms to all major software safety standards
- Mysql Json 数据类型&函数
- VoIP Push 在海外音视频业务中的应用
- PostgreSQL每周新闻—6月22日
- leetcode:307. 区域和检索 - 数组可修改
- "Xiaodeng" active directory password expiration notification function is available for operation and maintenance
- 解释PBR纹理贴图(texture-maps)
猜你喜欢

STL tutorial 6-deque, stack, queue, list container

Explain PBR texture maps
【云原生实战】KubeSphere实战——多租户系统实战

《强化学习周刊》第51期:PAC、ILQL、RRL&无模型强化学习集成于微电网络格控制:综述与启示

Application of VoIP push in overseas audio and video services

Stm32cubemx learning (6) external interrupt experiment

WIN10设置自动拨号联网任务,实现开机、断网自动重连

导航【微机原理】
![Navigation [microcomputer principle]](/img/79/8311a409113331e72f650a83351b46.png)
Navigation [microcomputer principle]

How to judge the quality of conductive slip ring from its appearance
随机推荐
About Effect Size
Website stress testing tool - webbench
leetcode:370. Interval addition
I found another cross domain method by chance. I don't know if anyone has ever played this way
Topic39——78. subset
TMUX settings
How do I audit Active Directory User account changes?
Db查询数据库合并两个不相关的表,新增不存在的字段,并赋予默认值
Explain PBR texture maps
MySQL JSON data types & functions
Win10 sets automatic dial-up networking task to realize automatic reconnection after startup and disconnection
Common methods of string class
yolov6训练自己的数据记录+yolov5对比测试
STL教程6-deque、stack、queue、list容器
Navigation [microcomputer principle]
导航 实验【微机原理】【实验】
【摸鱼神器】UI库秒变低代码工具——表单篇(一)设计
Logical structure and physical structure
Digital password lock Verilog design + simulation + on board verification
Reinforcement learning weekly (issue 51): integration of PAC, ilql, RRL & model free reinforcement learning into micro grid control: overview and Enlightenment