Ask Your Question

Undefined reference to class::function()

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

merosss gravatar image

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

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{
  int state;

  virtual ~Cao();    
  static void SayHello();   

#endif /* CAO_H_ */

And the Cao.cpp is like this:

#include "Cao.h"

  state = 0;

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



    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


show full error pls

BP gravatar imageBP ( 2014-04-05 09:08:54 -0600 )edit

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

merosss gravatar imagemerosss ( 2014-04-05 10:15:29 -0600 )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 imagemerosss ( 2014-04-05 22:18:17 -0600 )edit

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

BP gravatar imageBP ( 2014-04-05 23:28:38 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted

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

BP gravatar image

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

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

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

merosss gravatar image

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

As BP suggested it's clearly explained here: !

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools



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

Seen: 14,063 times

Last updated: Sep 19 '14