1.先建立一个新的功能包,参考过往文章。
2.功能包下的 include/功能包名 目录下新建头文件hello.h
#ifndef _HELLO_H
#define _HELLO_H
namespace hello_ns {
class My {
public:
void run();
};
}
#endif
3.建立hello.cpp文件
#include "test_manage/hello.h"
#include "ros/ros.h"
namespace hello_ns{
void My::run(){
ROS_INFO("hello,head and src ...");
}
}
4.为了后续包含头文件时不抛出异常,配置 .vscode 下 c_cpp_properties.json 的 includepath属性
5.建立文件main文件
#include "ros/ros.h"
#include "test_manage/hello.h"
int main(int argc, char *argv[])
{
ros::init(argc,argv,"hahah");
hello_ns::My my;
my.run();
return 0;
}
6.配置文件
6.1头文件与源文件相关配置::
6.2可执行文件配置:
7.编译运行
总结:
主要注意事项为配置问题,配置要点:
1. .vscode 下 c_cpp_properties.json 的 includepath属性,将功能包下的include路径添加进去并在后面加 /** 以便能搜索到这个目录下的多个头文件
2.头文件与源文件相关配置,让源文件与头文件链接起来,主要为
include_directories
add_library
add_dependencies
target_link_libraries
的配置
3.可执行文件配置,与之前的配置一样,主要为
add_executable
add_dependencies
target_link_libraries
的配置。