第三章 ROS通信机制进阶(3.2)

ROS通信机制进阶(3.2)

3.2 ROS中的头文件和源文件
本节主要介绍ROS的C++实现中,如何使用头文件与源文件的方式封装代码,具体内容如下:

1.设置头文件,可执行文件作为源文件;
2.分别设置头文件,源文件与可执行文件。
在ROS中关于头文件的使用,核心内容在于CMakeLists.txt文件的配置,不同的封装方式,配置上也有差异。
3.2.1自定义头文件调用
需求:设计头文件,可执行文件本身作为源文件。

流程:

(1)编写头文件;
(2)编写可执行文件(同时也是源文件);
(3)编辑配置文件并执行。
1.头文件
在功能包下的 include/功能包名 目录下新建头文件: hello.h,示例内容如下:

#ifndef  _HELLO_H
#define  _HELLO_H
/*
    声明namespace
         |--class
                |--run
*/ 
namespace hello_ns {

class My_Hello{
public:
    void run();
};

}

#endif

注意:在 VScode 中,为了后续包含头文件时不抛出异常,请配置 .vscode 下 c_cpp_properties.json 的 includepath属性

"/home/用户/工作空间/src/功能包/include/**"
"/home/ubuntu16/demo03_ws/src/plumbing_head/include/plumbing_head/**"

2.可执行文件:
在 src 目录下新建文件:hello.cpp,示例内容如下:

#include <ros/ros.h>
#include "plumbing_head/hello.h"

namespace hello_ns  {
    void My_Hello::run(){
        ROS_INFO("RUN函数执行....");
    }
}
int main(int argc, char  *argv[])
{
    setlocale(LC_ALL,"");
    ros::init(argc, argv, "hello_head");
    ros::NodeHandle  nh;
    hello_ns::My_Hello  my_Hello;
    my_Hello.run();
    return 0;
}

3.配置文件
配置CMakeLists.txt文件,头文件相关配置如下:

include_directories(
include
  ${catkin_INCLUDE_DIRS}
)

可执行配置文件配置方式与之前一致:

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

最后,编译并执行,控制台可以输出自定义的文本信息。

3.2.1自定义源文件调用
需求:设计头文件与源文件,在可执行文件中包含头文件。

流程:

(1)编写头文件;
(2)编写源文件;
(3)编写可执行文件;
(4)编辑配置文件并执行。
1.头文件:
头文件设置于 3.2.1 类似,在功能包下的 include/功能包名 目录下新建头文件: haha.h,示例内容如下:

#ifndef  _HELLO_H
#define  _HELLO_H   

/*
    声明namespace
        |--class
            |--run
*/
namespace   hello_ns{

class My_Hello{
public:
    void run();

};
}


#endif

注意:在 VScode 中,为了后续包含头文件时不抛出异常,请配置 .vscode 下 c_cpp_properties.json 的 includepath属性。
2.源文件:
在 src 目录下新建文件:hello.cpp,示例内容如下:

#include <ros/ros.h>
#include "plumbing_head_src/hello.h"

namespace  hello_ns
{
    void My_Hello::run(){
        ROS_INFO("源文件中的run函数....");
    }
}

3.可执行文件:
在 src 目录下新建文件: use_hello.cpp,示例内容如下:

#include <ros/ros.h>
#include "plumbing_head_src/hello.h"

int main(int argc, char  *argv[])
{
    setlocale(LC_ALL,"");
    ros::init(argc, argv, "hello_head__src");

    hello_ns::My_Hello  myHello;
    myHello.run();
    
    return 0;
}

4.配置文件:
头文件与源文件相关配置:

include_directories(
include
  ${catkin_INCLUDE_DIRS}
)

## Declare a C++ library  ##C++  library_name(defined byself)    head_file   source_file
add_library(head_src   
  include/plumbing_head_src/hello.h 
  src/hello.cpp 
)
##library_name
add_dependencies(head_src ${${PROJECT_NAME}_EXPORTED_TARGETS}    ${catkin_EXPORTED_TARGETS})
##library_name
target_link_libraries(head_src  
  ${catkin_LIBRARIES}
)

可执行文件配置:

add_executable(use_hello src/use_hello.cpp)
#此处需要添加之前设置的 head 库
##two param: first is executable_file_name, second is library_name
target_link_libraries(use_hello
  head_src
  ${catkin_LIBRARIES}
)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值