ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
2

c++ : one file class-in-node to three files class, node, and header

asked 2015-03-12 13:48:15 -0500

benabruzzo gravatar image

updated 2015-03-12 15:35:17 -0500

[Note: Prior to this question, my code and package compiled and functioned properly, the issue came from rearranging my file structure etc.]

I am trying to make a single node reusable by separating it into multiple files. This is probably more of a c++ question, but I'm not sure if i need to do something different in my code, or in my CMakeLists...

My original file had this structure:

#include <head_loc/manythings.h>
class ClassName
{
private:
    Multiple ClassVariables;
public:
  ClassName()    // Constructor for publishers etc..
    {    }
  output function1()     // do 1st thing
    {    }
  output functionN()     // do Nth thing
    {    }
};
int main(int argc, char** argv)
{
    ros::init(argc, argv, "ClassName_Node");
    ClassName CN;
    ros::spin();
    return 0;
}

My three new files have this structure: Node file:

#include <classheader.h>
int main(int argc, char** argv)
{
  ros::init(argc, argv, "ClassName_Node");
  ClassName CN;
  ros::spin();
  return 0;
}

Header file :

#ifndef _CLASSHEADER_H
#define _CLASSHEADER_H
#include <head_loc/manythings.h>
class ClassName
{
private:
  Multiple ClassVariables;
public:
  ClassName();    // Constructor for publishers etc..
  output function1();     // do 1st thing
  output functionN();     // do Nth thing
};
#endif

Class fucntions :

#include <classheader.h>
ClassName::ClassName()    // Constructor for publishers etc..
{    }
output ClassName::function1()     // do 1st thing
{    }
output ClassName::functionN()     // do Nth thing
{    }

I assumed this format to allow two different ROS nodes to call and use the same class. I get a compile error (truncated to what i believe is the relevant error codes) :

[100%] Building CXX object CMakeFiles/handrail_end_class.dir/src/handrail_end_class.cpp.o
Linking CXX executable /home/benjamin/r2_hydro/devel_isolated/lib/nasa_r2_vision_handrail/handrail_end_class
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 10
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 10
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 2
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 12
/usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 12
/usr/bin/ld: /usr ...
(more)
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
7

answered 2015-03-13 03:21:48 -0500

Wolf gravatar image

updated 2015-03-13 03:22:14 -0500

You have to change

add_executable(       class_node   src/class_node.cpp)
add_dependencies(     class_node   ${catkin_EXPORTED_TARGETS})
target_link_libraries(class_node   ${catkin_LIBRARIES}  ${PCL_LIBRARIES}  ${OpenCV_LIBRARIES})

add_executable(       class_functions   src/class_functions.cpp)
add_dependencies(     class_functions   ${catkin_EXPORTED_TARGETS})
target_link_libraries(class_functions   ${catkin_LIBRARIES}  ${PCL_LIBRARIES}  ${OpenCV_LIBRARIES})

either for

add_library(       class_functions_lib   src/class_functions.cpp)
add_dependencies(     class_functions_lib   ${catkin_EXPORTED_TARGETS})
target_link_libraries( class_functions_lib   ${catkin_LIBRARIES}  ${PCL_LIBRARIES}  ${OpenCV_LIBRARIES})

add_executable(       class_node   src/class_node.cpp)
add_dependencies(     class_node   ${catkin_EXPORTED_TARGETS})
target_link_libraries( class_node class_functions_lib   ${catkin_LIBRARIES}  ${PCL_LIBRARIES}  ${OpenCV_LIBRARIES})

or for

add_executable(       class_node   src/class_node.cpp  src/class_functions.cpp )
add_dependencies(     class_node   ${catkin_EXPORTED_TARGETS})
target_link_libraries( class_node ${catkin_LIBRARIES}  ${PCL_LIBRARIES}  ${OpenCV_LIBRARIES})

.

The first version creates you a lib ( shared object (.so) file ) and links your exec to your lib. The second version compiles boths source files to object files (.o) and links them both to an executable file containing compiled code of all your files.

edit flag offensive delete link more

Comments

This fixes it, thank you!

benabruzzo gravatar image benabruzzo  ( 2015-03-13 08:49:00 -0500 )edit

@Wolf thanks it worked great. I know this post is old now, but which of the two options do you recommend? Linking to a shared object (library) or linking to multiple object files?

jwin gravatar image jwin  ( 2019-01-03 02:52:28 -0500 )edit
0

answered 2015-03-12 14:02:37 -0500

Miquel Massot gravatar image

You have missed the link between the class functions and the class node.

target_link_libraries(class_node class_functions)
edit flag offensive delete link more

Comments

This seems like the appropriate route. Am I just making the changes incorrectly? [see edit made to original question]

benabruzzo gravatar image benabruzzo  ( 2015-03-12 15:26:06 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2015-03-12 13:48:15 -0500

Seen: 1,492 times

Last updated: Mar 13 '15