Ask Your Question
0

ROS node can not import a class

asked 2019-03-19 09:50:58 -0500

oiseau gravatar image

updated 2019-03-19 10:20:33 -0500

Hello everyone,

I am not sure that my problem is ROS related, but I have made a ROS node that was successfully working before I try to import a class inside it. Now, I get an undefined reference every catkin_make :

CMakeFiles/attachPoint.dir/src/attachPoint.cpp.o: In function `main':
attachPoint.cpp:(.text+0x4c5): undefined reference to `Magnet::Magnet(ros::NodeHandle)'
collect2: error: ld returned 1 exit status

I did try to modify the cmakelist to make it work, but I was not successfull.

This is my main where I get the error :

#include <ros/ros.h>
#include <tf/tf.h>
#include "magnet.h"

int main(int argc, char **argv)
{
    ros::init(argc, argv, "attachPoint");
    ros::NodeHandle nh;

    Magnet mgt = Magnet(nh);
[...]
}

Cmakelist :

cmake_minimum_required(VERSION 2.8.3)
project(approche)

find_package(catkin REQUIRED COMPONENTS
  message_generation
  roscpp
  rospy
  std_msgs
  mavros
  aruco_detect
  geometry_msgs
)

 add_message_files(
    FILES
    posmarker.msg
#   Message2.msg
 )

 generate_messages(
   DEPENDENCIES
   std_msgs
   geometry_msgs
 )

catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES approche
  CATKIN_DEPENDS roscpp std_msgs aruco_detect mavros message_runtime
#  DEPENDS system_lib
)

include_directories(
# include
  ${catkin_INCLUDE_DIRS}
)

add_executable(attachPoint src/attachPoint.cpp)
target_link_libraries(attachPoint ${catkin_LIBRARIES})

This the header file of my class magnet.h

#include <ros/ros.h>
#include <dynamic_reconfigure/IntParameter.h>
#include <std_msgs/Int16.h>

class Magnet
{
public:
    Magnet(ros::NodeHandle mynode);

    void boost(void);

    void off(void);

    void down(void);

    int getPWMBoost(void);

    void setPWMBoost(int mypwm);

    int getPWMDown(void);

    void setPWMDown(int mypwm);

private:
    ros::NodeHandle nh;
    ros::Publisher sendOrder_pub;

    std_msgs::Int16 data_magnet;
    void magnet_cb(const std_msgs::Int16::ConstPtr& data);
    ros::Subscriber magnet_sub;
    dynamic_reconfigure::IntParameter sendOrder;

    bool read;
};

Best regards.

edit retag flag offensive close merge delete

Comments

Please include the CMakeLists.txt file in your post!

christophebedard gravatar imagechristophebedard ( 2019-03-19 10:01:23 -0500 )edit

Done, don't worry about messages that are not including in the main, I just put the essentials here.

oiseau gravatar imageoiseau ( 2019-03-19 10:08:17 -0500 )edit

2 Answers

Sort by » oldest newest most voted
0

answered 2019-03-19 10:08:26 -0500

updated 2019-03-19 10:27:50 -0500

Try uncommenting # include so that you have

include_directories(
    include
    ${catkin_INCLUDE_DIRS}
)

This is assuming that magnet.h is directly under your include/ directory, and that there is indeed a constructor that accepts a NodleHandle. That error message seems to mean that you do not have one. The implementation should look like this in the .cpp file.

Magnet::Magnet(ros::NodeHandle mynode) {
    //...
}

Last thing: add magnet.cpp at the end in add_executable()! This is probably the cause.

edit flag offensive delete link more

Comments

I still get the same error.

oiseau gravatar imageoiseau ( 2019-03-19 10:11:25 -0500 )edit

Then can you post a minimal version of your magnet.h file and specify where it's located?

christophebedard gravatar imagechristophebedard ( 2019-03-19 10:13:30 -0500 )edit

This is quite small already. I did upload the entire file.

oiseau gravatar imageoiseau ( 2019-03-19 10:22:42 -0500 )edit

Alright. I edited my answer above. Please check your .cpp file/implementation!

christophebedard gravatar imagechristophebedard ( 2019-03-19 10:23:28 -0500 )edit

This is a linker error, not a compiler error. #include and include paths are all compiler/preprocessor pieces of infrastructure.

gvdhoorn gravatar imagegvdhoorn ( 2019-03-19 10:27:58 -0500 )edit

My class is in the src file of my package, I did have the implementation of my constructor.

Magnet::Magnet(ros::NodeHandle mynode){
    nh = mynode;
    sendOrder_pub = nh.advertise<dynamic_reconfigure::IntParameter> ("/magnet/command", 10);
    magnet_sub = nh.subscribe<std_msgs::Int16> ("/magnet/value", 10, &Magnet::magnet_cb, this);
    read = true;
}
oiseau gravatar imageoiseau ( 2019-03-19 10:36:40 -0500 )edit

@gvdhoorn so the problem is in add_executable()?

christophebedard gravatar imagechristophebedard ( 2019-03-19 11:00:33 -0500 )edit
0

answered 2019-03-20 04:45:17 -0500

oiseau gravatar image

I got the answer and that was pretty simple. I only needed to change this line in the cmakelist :

add_executable(attachPoint src/attachPoint.cpp src/magnet.cpp)
target_link_libraries(attachPoint ${catkin_LIBRARIES})
edit flag offensive delete link more

Comments

Yes, usually you list all of your .cpp files in add_executable(). Only noticed that after everything else!

christophebedard gravatar imagechristophebedard ( 2019-03-22 18:07:43 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2019-03-19 09:50:58 -0500

Seen: 95 times

Last updated: Mar 20