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

Undefined reference to class::function()

asked 2014-04-05 04:37:58 -0500

merosss gravatar image

updated 2014-04-05 22:25:58 -0500

Hi everybody, I'm a little bit new to ROS and making classes in c++ so excuse me for all the crap I'll surely write. My problem is that: I want to write a class called Cao and call its function from the Ros file which contains the main, but when I try to catkin_make it gives me the error: undefined reference to `Cao::SayHello()'.

Since the absence of a main I've declared the class a library adding this line in the CMakeLists.txt:

add_library(quadcopter_ctrl src/Cao.cpp)

The header Cao.h looks like this:

#ifndef CAO_H_
#define CAO_H_

#include <iostream>

class Cao{
private:
  int state;

public:
  Cao();
  virtual ~Cao();    
  static void SayHello();   
};

#endif /* CAO_H_ */

And the Cao.cpp is like this:

#include "Cao.h"

Cao::Cao(){
  state = 0;
}

Cao::~Cao(){
  // TODO Auto-generated destructor stub
}

void Cao::SayHello(){
  std::cout << "Hello World!";
  std::cout << std::endl;
}

I'm calling the SayHello function in this quadcopterRosCtrl.cpp file:

#include "ros/ros.h"
#include "geometry_msgs/PoseStamped.h"
#include "std_msgs/String.h"
#include "../include/quadcopter_ctrl/termColors.h"
#include "../include/quadcopter_ctrl/quadcopterRosCtrl.h"
#include "Cao.h"

geometry_msgs::PoseStamped quadPos;
int quadPosAcquired = 0;

void quadPosFromVrep(const geometry_msgs::PoseStamped::ConstPtr& pubQuadPose)
{
    quadPos.pose.position.x = pubQuadPose->pose.position.x;
    quadPos.pose.position.y = pubQuadPose->pose.position.y;
    quadPosAcquired = 1;    
}

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

    ros::Publisher targetObjPos_pub = n.advertise<geometry_msgs::PoseStamped>("vrep/targetObjPos", 100);
    ros::Subscriber quadcopPos_sub = n.subscribe("vrep/quadcopPos", 100, quadPosFromVrep);

    Cao::SayHello();

    [ SOME CODE IN THE MIDDLE ]

    return 0;
}

And I didn't manage to get rid of this error in any way. It's something related to the code or to the CMakeList.txt?? The full output error after catkin_make is the following:

CMakeFiles/quadcopterRosCtrl.dir/src/quadcopterRosCtrl.cpp.o: In function `main':
/home/francescow/catkin_ws/src/quadcopter_ctrl/src/quadcopterRosCtrl.cpp:103: undefined reference to `Cao::SayHello()'
collect2: ld returned 1 exit status
make[2]: *** [/home/francescow/catkin_ws/devel/lib/quadcopter_ctrl/quadcopterRosCtrl] Error 1
make[1]: *** [quadcopter_ctrl/CMakeFiles/quadcopterRosCtrl.dir/all] Error 2
make: *** [all] Error 2

Thanks in advance.

edit retag flag offensive close merge delete

Comments

show full error pls

BP gravatar image BP  ( 2014-04-05 09:08:54 -0500 )edit

Just edited. I've made some slight changes but error is always the same.

merosss gravatar image merosss  ( 2014-04-05 10:15:29 -0500 )edit

Thanks BP, it worked!!! Although I was missing some few other things, like where to put the linking instruction, and the meaning of the syntax, but definitely that was the problem. (Can I answer clearly my own question just for the records or that's enough?)

merosss gravatar image merosss  ( 2014-04-05 22:18:17 -0500 )edit

I don't know. In future try to look up things in tutorials (for instance CMakeList - there are also descriptions).

BP gravatar image BP  ( 2014-04-05 23:28:38 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
5

answered 2014-04-05 10:34:28 -0500

BP gravatar image

updated 2014-04-05 10:35:02 -0500

You probably forgot about target_link_libraries(your_executable_name quadcopter_ctrl) in CMakeList (I assume you've set include path correctly). If this wont work, posted CMakeList can give me some clues maybe.

edit flag offensive delete link more
1

answered 2014-04-06 00:00:41 -0500

merosss gravatar image

updated 2014-09-19 09:33:31 -0500

As BP suggested it's clearly explained here: http://wiki.ros.org/catkin/CMakeLists... !

edit flag offensive delete link more

Question Tools

3 followers

Stats

Asked: 2014-04-05 04:37:58 -0500

Seen: 21,074 times

Last updated: Sep 19 '14