0
$\begingroup$

Rosanswers logo

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.


Originally posted by merosss on ROS Answers with karma: 723 on 2014-04-05

Post score: 3


Original comments

Comment by BP on 2014-04-05:
show full error pls

Comment by merosss on 2014-04-05:
Just edited. I've made some slight changes but error is always the same.

Comment by merosss on 2014-04-05:
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?)

Comment by BP on 2014-04-05:
I don't know. In future try to look up things in tutorials (for instance CMakeList - there are also descriptions).

$\endgroup$

2 Answers 2

0
$\begingroup$

Rosanswers logo

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.


Originally posted by BP with karma: 176 on 2014-04-05

This answer was ACCEPTED on the original site

Post score: 5

$\endgroup$
0
$\begingroup$

Rosanswers logo

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


Originally posted by merosss with karma: 723 on 2014-04-06

This answer was NOT ACCEPTED on the original site

Post score: 1

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.