• 【ROS进阶篇】第二讲 自定义头、源文件封装


    【ROS进阶篇】第二讲 自定义头、源文件封装

    在这里插入图片描述

    前言

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

    在这里插入图片描述

    自定义头文件封装

    1. 流程

    • 概念:自行设计头文件和可执行文件,并将可执行文件本身作为源文件
    • 流程:
    1. 编写头文件
    2. 编写可执行文件/作为源文件
    3. 修改编译规则配置文件编译

    2. 头文件

    • 代码演示:
    #ifndef _HELLO_H
    #define _HELLO_H
    
    namespace hello_ns{
    
    class HelloPub {
    
    public:
        void run();
    };
    
    }
    
    #endif
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 简要分析:
    1. 宏定义与预处理防止头文件的重复包含和编译
    #ifndef _HELLO_H     //先测试x是否被宏定义过
    #define _HELLO_H    //程序段1 //如果x没有被宏定义过,定义x,并编译程序段 1
    ...
    #endif      					//终止if
    
    • 1
    • 2
    • 3
    • 4
    1. 命名空间防止出现标识符之间的冲突,区分类似的函数、类、变量
    namespace hello_ns{
    ...
    }
    
    • 1
    • 2
    • 3
    • c_cpp_properties.json:修改includepath属性防止抛出异常;
    "/home/用户/工作空间/src/功能包/include/**"
    
    • 1

    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    4. 修改编译规则

    • 添加include:
    include_directories(
    include
      ${catkin_INCLUDE_DIRS}
    )
    
    • 1
    • 2
    • 3
    • 4
    • 配置文件依赖项与链接:
    add_executable(hello src/hello.cpp)
    
    add_dependencies(hello ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
    
    target_link_libraries(hello
      ${catkin_LIBRARIES}
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    自定义源文件封装

    1. 流程

    • 概念:分别定义源文件、头文件、可执行文件,在可执行文件中包含头文件
    • 流程:
    1. 编写头文件;
    2. 编写源文件;
    3. 编写可执行文件;
    4. 编写配置文件并执行;

    2. 头文件

    • 代码展示:head.h
    #ifndef _HEAD_H
    #define _HEAD_H
    
    namespace hello_ns {
    
    class My {
    
    public:
        void run();
    
    };
    
    }
    
    #endif
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 文件目录:功能包下的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 ...");
    }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    5. 修改编译规则

    • 头文件与源文件:
    1. include项:
    include_directories(
    include
     ${catkin_INCLUDE_DIRS}
    )
    
    • 1
    • 2
    • 3
    • 4
    1. 声明C++库
    add_library(head
     include/test_head_src/head.h
     src/source.cpp
    )
    
    • 1
    • 2
    • 3
    • 4
    1. 依赖项与链接
    add_dependencies(head >${${PROJECT_NAME}_EXPORTED_TARGETS} >${catkin_EXPORTED_TARGETS})
    
    target_link_libraries(head
     ${catkin_LIBRARIES}
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 可执行文件:

    依赖项与链接:

    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}
    )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    总结

    • 声明:本节博客部分参考了CSDN用户赵虚左的ROS教程,下篇博客将会主要聚焦于ROS的运行管理上,介绍元功能包与工作空间覆盖的概念。

    在这里插入图片描述

  • 相关阅读:
    vue3 - 项目集成vue-i18n国际化和Element Plus 国际化
    操作系统的运行机制
    对话钱江机器人丨国产化破风,谁动了工业机器人厂商的“奶酪”?
    嵌入式FreeRTOS学习六,FreeRTOS中CPU寄存器与RAM内存和Flash之间的数据传输,以及栈空间的作用
    【springboot】17、使用/注入Servlet、Filter、Listener
    PyQt5自定义信号
    list根据对象中某个字段属性去重Java流实现
    Nignx部署前端页面
    axios上传文件错误:Current request is not a multipart request
    Lambda表达式
  • 原文地址:https://blog.csdn.net/lc1852109/article/details/125466588