Rosanswers logo

Hi everyone, I am trying to have .h and .cpp files in my ros package. All my code is in the src file and I have the following code. The code works fine when I run through xcode so the syntax is okay however ros does not like it for some unknown reason: Here is the .h file

#include <iostream>
class DroneCommands{


void Land(int time);

  #endif /* defined(__helloworld__DroneCommands__) */

Here is the .cpp file:

#include "DroneCommands.h"
#include <iostream>

std::cout << "object created" << std::endl;

void DroneCommands::Land(int time){
std::cout << "Landing!!" << std::endl;


Finally, here is main.cpp: #include #include "DroneCommands.h"

int main(int argc, char **argv)
DroneCommands dc; 


The command prompt error is: undefined reference to `DroneCommands::DroneCommands()'

Please let me know why this does not work even though I can get it to run on Xcode successfully. Thanks :)

Originally posted by JP on ROS Answers with karma: 95 on 2013-10-08

Post score: 0

Original comments

Comment by BennyRe on 2013-10-08:
Is your header file located in the src folder or in include/?

Comment by JP on 2013-10-09:
Hey, it was in the src folder.

Comment by BennyRe on 2013-10-09:
Are you linking the both source files together? Please post your CMakeLists.txt

Comment by JP on 2013-10-09:
I only did : rosbuild_add_executable(main src/main.cpp). So I need to add rosbuild_add_executable(DroneCommandssrc/DroneCommands.cpp) also?


1 Answer 1


Rosanswers logo

Do the following in your CMakeLists.txt:

rosbuild_add_executable(main src/main.cpp DroneCommandssrc/DroneCommands.cpp)

Originally posted by BennyRe with karma: 2949 on 2013-10-09

This answer was ACCEPTED on the original site

Post score: 1

Original comments

Comment by JP on 2013-10-10:
Brilliant, thanks!!

Comment by JP on 2013-10-11:
Hi, I tried: rosbuild_add_executable(main src/main.cpp DroneCommands src/X/DroneCommands.cpp)

The reason I have X is because my DroneCommands.cpp file is in a folder called X. Is it not possible to separate the .cpp files into different files because that seems to be the error.

Comment by BennyRe on 2013-10-11:
remove the DroneCommands only use: rosbuild_add_executable(main src/main.cpp src/X/DroneCommands.cpp)

Comment by JP on 2013-10-12:
Thank you!


Your Answer

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