undefined Reference to ros::init on kinetic in non-ros-domain workspace
Hi All,
I am trying to compile a .cpp file which uses some of the ROS library functions like "ros/ros.h" , "std_msgs/String.h". In the make file , I have included the path for the respective include files in the include section.
But when I am compiling the .cpp , I am getting errors like
Undefined reference to 'ros::init(int&, char**, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, unsigned int)'.
Like wise I am getting some other errors like
:Undefined reference to ros::NodeHandle
:Undefined reference to ros::Rate::Rate(double)
:Undefined reference to ros::ok()
etc.
What could be the reason for such errors? can I get some suggestion to overcome these errors?
Thank you for your suggestion in advance?
"Undefined reference" is probably a linker error. You don't just need to add directories for searching for headers, you also need to add directories for the linker to search in (
-L
), and you need to specify which libraries to search in when linking (-l
).This is partially why tools like CMake/catkin exist - writing a Makefile for complex projects can be tough. It might help if we could see your Makefile or your compile command.
Hello @jarvisscchultz But I could not figure out which exact libraries are being used while I am creating .cpp file as a node in a ros package in catkin_ws.
It uses std_msgs and roscpp in the find_package() section of CMakeLists.txt. Is there any way to find out the libs used by the created ros package in the catkin_ws.
Not sure I understand your question.
find_package
only creates variables that can be used later. For example, if youfind_package
std_msgs
androscpp
then you would be able to usetarget_link_libraries
with thecatkin_LIBRARIES
variable to link against all libs found infind_package
Again, it would be helpful to actually see your Makefile. One other thought that might help is using
ldd
to test what libraries are dynamically loaded when running an executable. E.g.ldd /home/user/catkin_ws/devel/lib/package_name/executable_name
Thank you so much @jarvisschultz. ldd command really helped me a lot. I could able to run my .cpp ros program outside of catkin_ workspace. I was struggling for last 2 weeks.