需求
设计头文件,可执行文件本身作为源文件。
流程
-
编写头文件;
-
编写可执行文件(同时也是源文件);
-
编辑配置文件并执行。
1.头文件代码
在功能包下的 include/功能包名 目录下新建头文件: hello.h,示例内容如下:
#ifndef _HELLO_H
#define _HELLO_H
namespace hello_ns{
class HelloPub {
public:
void run();
};
}
#endif
注意:
在 VScode 中,为了后续包含头文件时不抛出异常,请配置 .vscode 下 c_cpp_properties.json 的 includepath属性
"/home/用户/工作空间/src/功能包/include/**"
2.可执行文件
在 src 目录下新建文件:hello.cpp,示例内容如下:
#include "ros/ros.h"
#include "test_head/hello.h"
namespace hello_ns {
void HelloPub::run(){
ROS_INFO("自定义头文件的使用