怎么在ros功能包下添加自己写的的头文件

参考链接
基于VSCode和CMake实现C/C++开发 | Linux篇

根据以上视频第七讲的内容把它写在了ros功能包里面
代码结构如下

在这里插入图片描述
相关代码如下:
Gun.h

#pragma once

#include <iostream>
#include <string>

//类的声明大括号后面要加分号
class Gun
{
public:
    //构造函数赋值
    Gun(std::string type)
    {
        this->_bullet_cout = 0;
        this->_type = type;
    }
    //接口
    void addBullet(int bullet_num);
    bool shoot();

private:
    int _bullet_cout;
    std::string _type;
};

solider.h

#pragma once
#include <iostream>
#include <string>
#include "Gun.h"

class solider
{
public:
    solider(std::string name);
    ~solider();
    void addBulletToGun(int num);
    void addGun(Gun *ptr_gun);
    bool fire();

private:
    std::string _name;
    Gun *_ptr_gun;
};

Gun.cpp

#include <Gun.h>

using namespace std;

void Gun::addBullet(int bullet_num)
{
    this->_bullet_cout += bullet_num;
}
bool Gun::shoot()
{
    if (this->_bullet_cout <= 0)
    {
        cout << "no bullet" << endl;
        return false;
    }
    this->_bullet_cout -=1;
    cout<<"sucess"<<endl;
    return true;
}

solider.cpp

#include "Gun.h"
#include "solider.h"

void test()
{
    solider sanduo("xusanduo");
    sanduo.addGun(new Gun("AK47"));
    sanduo.addBulletToGun(20);
    sanduo.fire();
}

int main(int argc, char **argv)
{
    /* code */
    test();

    return 0;
}

main.cpp

#include "Gun.h"
#include "solider.h"

void test()
{
    solider sanduo("xusanduo");
    sanduo.addGun(new Gun("AK47"));
    sanduo.addBulletToGun(20);
    sanduo.fire();
}

int main(int argc, char **argv)
{
    /* code */
    test();

    return 0;
}

之后配置src目录下的CMakeLists.txt文件
添加以下内容

include_directories(include) //这里是指相对路径
add_executable(my_c_exe src/main.cpp src/Gun.cpp src/solider.cpp )
add_dependencies(my_c_exe ${${PROJECT_NAME}_EXPORTED_TARGETS} ${catkin_EXPORTED_TARGETS})
target_link_libraries(my_c_exe
  ${catkin_LIBRARIES}
)

之后进行catkin_make
编译成功后
运行

rosrun 7sample my_c_exe 

在这里插入图片描述

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值