Bug_Free
Bug_Free

Reputation: 81

Using external classes in cpp ros nodes

I have a directory in my ros package directory besides src and include directories, in which my class header and source files are:

my_dir/my_class

I have written a ros node in cpp in src directories. I have created an object in this node. How should I configure the cmake.txt and package.xml to know about this class definition?

I just want to have class definition outside of the node file!

kinetic ros - ubuntu 16.04 - roscpp

Upvotes: 3

Views: 2198

Answers (2)

Fruchtzwerg
Fruchtzwerg

Reputation: 11389

You don't need to modify your package.xml but you need to modify your CMakeLists.txt.

Add the additional include directory (in your case: my_dir)

include_directories(
  include ${catkin_INCLUDE_DIRS} my_dir
)

This allows to include the header like

#include <my_class.hpp>

To build the source files to your node or library just replace the common src directory to your specific directory (in your case: my_dir)

add_executable(your_node
  src/your_node.cpp
  my_dir/my_class.cpp
)

Upvotes: 7

Fareanor
Fareanor

Reputation: 6805

If you created your package with catkin_create_pkg, normally you just have to call catkin_make to build your project, you have nothing to write manually in the package.xml.
To find your class defined in other files, just include the header file in your node, exactly as simple C++ program because it is.

Regarding the CMakeLists.txt, see here, the last lines will give you how to add an executable. The CMakeLists.txt is automatically generated and you have a lots of explanations commented inside, check it out. It explains everything you need with examples, it is very well done.

Moreover, you can check the ros tutorials that was my reference to learn all the basics about ROS.

Finally, you have a much better specialized website than stackoverflow for ROS question, it is ros answers. It is a gold mine for ROS users.

I hope you will find your answers :)

Upvotes: 1

Related Questions