Ask Your Question

unidentified reference issue when having .h and .cpp files

asked 2013-10-08 12:14:47 -0500

JP gravatar image

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 <iostream> #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 :)

edit retag flag offensive close merge delete


Is your header file located in the src folder or in include/<package name="">?

BennyRe gravatar image BennyRe  ( 2013-10-08 23:54:19 -0500 )edit

Hey, it was in the src folder.

JP gravatar image JP  ( 2013-10-09 06:10:49 -0500 )edit

Are you linking the both source files together? Please post your CMakeLists.txt

BennyRe gravatar image BennyRe  ( 2013-10-09 21:38:55 -0500 )edit

I only did : rosbuild_add_executable(main src/main.cpp). So I need to add rosbuild_add_executable(DroneCommandssrc/DroneCommands.cpp) also?

JP gravatar image JP  ( 2013-10-09 22:34:25 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2013-10-09 22:43:46 -0500

BennyRe gravatar image

Do the following in your CMakeLists.txt:

rosbuild_add_executable(main src/main.cpp DroneCommandssrc/DroneCommands.cpp)
edit flag offensive delete link more


Brilliant, thanks!!

JP gravatar image JP  ( 2013-10-10 10:27:19 -0500 )edit

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.

JP gravatar image JP  ( 2013-10-11 00:55:15 -0500 )edit

remove the DroneCommands only use: rosbuild_add_executable(main src/main.cpp src/X/DroneCommands.cpp)

BennyRe gravatar image BennyRe  ( 2013-10-11 03:37:50 -0500 )edit

Thank you!

JP gravatar image JP  ( 2013-10-12 06:31:19 -0500 )edit

Your Answer

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

Add Answer

Question Tools

1 follower


Asked: 2013-10-08 12:14:47 -0500

Seen: 149 times

Last updated: Oct 09 '13