Ask Your Question
0

unidentified reference issue when having .h and .cpp files

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

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{

public:

DroneCommands();
void Land(int time);



 };
  #endif /* defined(__helloworld__DroneCommands__) */

Here is the .cpp file:

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


DroneCommands::DroneCommands(){
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; 
dc.Land(3);

}

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

Comments

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

BennyRe gravatar imageBennyRe ( 2013-10-08 23:54:19 -0600 )edit

Hey, it was in the src folder.

JP gravatar imageJP ( 2013-10-09 06:10:49 -0600 )edit

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

BennyRe gravatar imageBennyRe ( 2013-10-09 21:38:55 -0600 )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 imageJP ( 2013-10-09 22:34:25 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
1

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

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

Comments

Brilliant, thanks!!

JP gravatar imageJP ( 2013-10-10 10:27:19 -0600 )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 imageJP ( 2013-10-11 00:55:15 -0600 )edit

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

BennyRe gravatar imageBennyRe ( 2013-10-11 03:37:50 -0600 )edit
1

Thank you!

JP gravatar imageJP ( 2013-10-12 06:31:19 -0600 )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

Stats

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

Seen: 115 times

Last updated: Oct 09 '13