Search code examples
c++ros

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

  • my_class.cpp

  • my_class.hpp

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


Solution

  • 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
    )