How to use custom c++ class/oject

asked 2018-04-22 16:49:31 -0500

AC3021 gravatar image

I am trying to use a custom object in my ROS program but having issues linking everything.

I have seen older answers from ~6 years ago and have not seen any answers on how to do this in ROS indigo/kinetic. I have tried those answers and it did not work so my guess is something has changed in the catkin build requirements.

So my question is:

1) How to use custom objects in my ROS packages? and what is the best way if I want to use this object in multiple ROS packages.

// my_class.h
class MyClass {
    public:
        int a;
        int b;
        int addNumbers() const;
    }

// my_class.cpp
#include "my_class.h"
void MyClass::addNumbers() const {
    return a + b;
    }
edit retag flag offensive close merge delete

Comments

2

This is standard CMake stuff. You need to create a new ROS package that builds a library and use it from your other nodes. Either look at a CMake tutorial to learn how to add additional libraries, or post the errors you are getting.

Geoff gravatar image Geoff  ( 2018-04-22 18:39:52 -0500 )edit

There is a quite complete catkin documentation that should answer all your questions.

Other than that, I'm seconding @Geoff.

mgruhler gravatar image mgruhler  ( 2018-04-23 00:56:35 -0500 )edit

or post the errors you are getting.

+1000; I cannot emphasise this enough.

@AC3021: how do you expect us to help you if you don't show/tell us what sort of "issues linking" you have?

Not a lot changed in the past 6 years that influences this. As @Geoff writes: this is CMake, not Catkin.

gvdhoorn gravatar image gvdhoorn  ( 2018-04-23 00:59:45 -0500 )edit