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

ROS node can not import a class

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

oiseau gravatar image

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

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)

find_package(catkin REQUIRED COMPONENTS

#   Message2.msg


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

# include

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

    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


Please include the CMakeLists.txt file in your post!

christophebedard gravatar image christophebedard  ( 2019-03-19 10:01:23 -0600 )edit

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

oiseau gravatar image oiseau  ( 2019-03-19 10:08:17 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted

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

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


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

christophebedard gravatar image christophebedard  ( 2019-03-22 18:07:43 -0600 )edit

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

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

Try uncommenting # include so that you have


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


I still get the same error.

oiseau gravatar image oiseau  ( 2019-03-19 10:11:25 -0600 )edit

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

christophebedard gravatar image christophebedard  ( 2019-03-19 10:13:30 -0600 )edit

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

oiseau gravatar image oiseau  ( 2019-03-19 10:22:42 -0600 )edit

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

christophebedard gravatar image christophebedard  ( 2019-03-19 10:23:28 -0600 )edit

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

gvdhoorn gravatar image gvdhoorn  ( 2019-03-19 10:27:58 -0600 )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 image oiseau  ( 2019-03-19 10:36:40 -0600 )edit

@gvdhoorn so the problem is in add_executable()?

christophebedard gravatar image christophebedard  ( 2019-03-19 11:00:33 -0600 )edit

Question Tools

1 follower


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

Seen: 821 times

Last updated: Mar 20 '19