当前位置:网站首页>ROS中的头文件与源文件
ROS中的头文件与源文件
2022-07-27 05:21:00 【三个刺客】
本节主要介绍ROS的C++实现中,如何使用头文件与源文件的方式封装代码,具体内容如下:
- 设置头文件,可执行文件作为源文件;
- 分别设置头文件,源文件与可执行文件。
在ROS中关于头文件的使用,核心内容在于CMakeLists.txt文件的配置,不同的封装方式,配置上也有差异。
自定义头文件调用
需求:设计头文件,可执行文件本身作为源文件。
流程:
- 编写头文件;
- 编写可执行文件(同时也是源文件);
- 编辑配置文件并执行。
1.头文件
在功能包下的 include/功能包名 目录下新建头文件: hello.h,示例内容如下:
#ifndef _HELLO_H
#define _HELLO_H
/*
声明 namespace
class
run
*/
namespace hello_ns{
class MyHello{
public:
void run();
};
}
#endif注意:
在 VScode 中,为了后续包含头文件时不抛出异常,请配置 .vscode 下 c_cpp_properties.json 的 includepath属性
"/home/用户/工作空间/src/功能包/include/**"
"/home/mxh2/catkin_ws/src/plumbing_head/include/**",2.可执行文件
在 src 目录下新建文件:hello.cpp,示例内容如下:
#include "ros/ros.h"
#include "plumbing_head/hello.h"
namespace hello_ns{
void MyHello::run(){
ROS_INFO("run 函数执行.....");
}
}
int main(int argc, char *argv[])
{
setlocale(LC_ALL,"");
ros::init(argc,argv,"hello_head");
//函数调用
hello_ns::MyHello myHello;
myHello.run();
return 0;
}
3.配置文件
配置CMakeLists.txt文件,头文件相关配置如下:
include_directories(
include
${catkin_INCLUDE_DIRS}
)
可执行配置文件配置方式与之前一致:
add_executable(hello src/hello.cpp)
add_dependencies(hello ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(hello
${catkin_LIBRARIES}
)
自定义源文件调用
需求:设计头文件与源文件,在可执行文件中包含头文件。
流程:
- 编写头文件;
- 编写源文件;
- 编写可执行文件;
- 编辑配置文件并执行。
1.头文件
和上面的一样
注意:
在 VScode 中,为了后续包含头文件时不抛出异常,请配置 .vscode 下 c_cpp_properties.json 的 includepath属性
"/home/mxh2/catkin_ws/src/plumbing_head_src/include/**"2.源文件
#include "plumbing_head_src/hello.h"
#include "ros/ros.h"
namespace hello_ns{
void MyHello::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::MyHello myHello;
myHello.run();
return 0;
}
4.配置文件
include_directories(
include
${catkin_INCLUDE_DIRS}
## Declare a C++ library
add_library(head_src
include/${PROJECT_NAME}/hello.h
src/hello.cpp
)
add_dependencies(head_src ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(head_src
${catkin_LIBRARIES}
)
可执行文件配置:
add_executable(use_hello src/use_hello.cpp)
add_dependencies(use_hello ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(use_hello
head_src
${catkin_LIBRARIES}
)边栏推荐
- Code implementation and introduction of all commonly used sorting
- [song] rebirth of me in py introduction training (5): List
- C语言-文件操作
- 力扣题解 二叉树(8)
- IOT operating system
- Unity 引擎开始从 Mono 迁移到 .NET CoreCLR
- Man moon myth reading notes
- Leetcode每日一题30. 串联所有单词的子串
- Solve binary tree (6)
- What has been updated in the Chinese version of XMIND mind map 2022 v12.0.3?
猜你喜欢
随机推荐
Weidongshan digital photo frame project learning (III) transplantation of freetype
Li Kou daily question sword finger offer II 091. paint the house
编程学习记录——第7课【函数】
Reading and writing of C # file
SQL novice
QGIS系列(1)-QGIS(server-apache) win10安装
性感素数(Acwing每日一题)
Dynamic planning for solving problems (6)
Solve binary tree (5)
这是我的博客
Operate access database based on WinForm of C (at the end of MDB)
关于druid连接不上数据库的问题
常见的SQL优化方法
发布 分辨率0.22m的建筑物分割数据库
[first song] rebirth of me in py introductory training (4): Circular program
Dynamic planning for solving problems (3)
力扣 236. 二叉树的最近公共祖先
Force buckle 160. intersecting linked list
Essential tool for making video special effects: nuke 13
[song] rebirth of me in py introductory training (8): module









