unidentified reference issue when having .h and .cpp files
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 :)
Is your header file located in the src folder or in include/<package name="">?
Hey, it was in the src folder.
Are you linking the both source files together? Please post your CMakeLists.txt
I only did : rosbuild_add_executable(main src/main.cpp). So I need to add rosbuild_add_executable(DroneCommandssrc/DroneCommands.cpp) also?