(12)ROS学习-头文件源文件分离与管理

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

的配置。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值