Undefined reference to ros::init
Most of the similar questions pointed to the package's CMakeList.txt . As far as I can tell, it's as it should be:
cmake_minimum_required(VERSION 2.8.3)
project(random_walk)
find_package(catkin REQUIRED COMPONENTS
roscpp
geometry_msgs
sensor_msgs
)
message("catkin libraries" ${catkin_LIBRARIES})
catkin_package()
include_directories(include ${catkin_INCLUDE_DIRS})
add_executable(random_walk src/random_walk.cpp)
target_link_libraries(random_walk ${catkin_LIBRARIES})
add_dependencies(random_walk beginner_tutorials_generate_messages_cpp)
Packages.xml has the dependencies
<buildtool_depend>catkin</buildtool_depend>
<build_depend>roscpp</build_depend>
<build_depend>geometry_msgs</build_depend>
<build_depend>sensor_msgs</build_depend>
<run_depend>roscpp</run_depend>
<run_depend>geometry_msgs</run_depend>
<run_depend>sensor_msgs</run_depend>
Rosdep said all dependencies are installed. I performed a clean prior to trying to make, but I can't compile something as simple as:
#include "ros/ros.h"
int main (int argc, char **argv ) {
ros::init(argc, argv, "random_walk");
}
Without seeing
In function
main': random_walk.cpp:(.text+0x46): undefined reference to
ros::init(int&, char**, std::string const&, unsigned int)'
This source says "If you see errors from catkin_make that the header ros/ros.h cannot be found, or “undefined reference” errors on ros::init or other ROS functions, the most likely reason is that your CMakeLists.txt does not correctly declare a dependency on roscpp." I can't figure out what I'm missing. Any ideas?
Verbose Output of Make
/usr/bin/cmake -H/home/gary/catkin_ws/src -B/home/gary/catkin_ws/build --check-build-system CMakeFiles/Makefile.cmake 0 make -f CMakeFiles/Makefile2 random_walk make[1]: Entering directory `/home/gary/catkin_ws/build' /usr/bin/cmake -H/home/gary/catkin_ws/src -B/home/gary/catkin_ws/build --check-build-system CMakeFiles/Makefile.cmake 0 /usr/bin/cmake -E cmake_progress_start /home/gary/catkin_ws/build/CMakeFiles 1 make -f CMakeFiles/Makefile2 random_walk/CMakeFiles/random_walk.dir/all make[2]: Entering directory `/home/gary/catkin_ws/build' make -f random_walk/CMakeFiles/random_walk.dir/build.make random_walk/CMakeFiles/random_walk.dir/depend make[3]: Entering directory `/home/gary/catkin_ws/build' cd /home/gary/catkin_ws/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/gary/catkin_ws/src /home/gary/catkin_ws/src/random_walk /home/gary/catkin_ws/build /home/gary/catkin_ws/build/random_walk /home/gary/catkin_ws/build/random_walk/CMakeFiles/random_walk.dir/DependInfo.cmake --color= make[3]: Leaving directory `/home/gary/catkin_ws/build' make -f random_walk/CMakeFiles/random_walk.dir/build.make random_walk/CMakeFiles/random_walk.dir/build make[3]: Entering directory `/home/gary/catkin_ws/build' Linking CXX executable /home/gary/catkin_ws/devel/lib/random_walk/random_walk cd /home/gary/catkin_ws/build/random_walk && /usr/bin/cmake -E cmake_link_script CMakeFiles/random_walk.dir/link.txt --verbose=1 /usr/bin/c++ CMakeFiles/random_walk.dir/src/random_walk.cpp.o -o /home/gary/catkin_ws/devel/lib/random_walk/random_walk -rdynamic /opt/ros/indigo/lib/libroscpp_serialization.so /opt/ros/indigo/lib/librostime.so -lboost_date_time /opt/ros/indigo/lib/libcpp_common.so -lboost_system -lboost_thread -lpthread -lconsole_bridge -Wl,-rpath,/opt/ros/indigo/lib CMakeFiles/random_walk.dir/src/random_walk.cpp.o: In function `main': random_walk.cpp:(.text+0x46): undefined reference to `ros::init(int&, char**, std::string const&, unsigned int)' collect2: error: ld returned 1 exit status make[3]: *** [/home/gary/catkin_ws/devel/lib/random_walk/random_walk] Error 1 make[3]: Leaving directory `/home/gary/catkin_ws/build ...
catkin_make -j1 your_package_name VERBOSE=1 &> temp.txt
and inspect temp.txt to see if the libraries are in it. Also you could add amessage("catkin libraries " ${catkin_LIBRARIES})
to your CMakeLists.txt and inspect that (it is less messy than looking at the /usr/bin/c++ line.I would also use
#include <ros/ros.h>
(note the<>
instead of""
) Edit: Nvm, that shouldn't matter.@lucasw I added the verbose output, not exactly sure what I'm looking for. Interesting note, this project was in a new workspace I created; I tried again in the workspace I created in the tutorials and it worked. I went back through the tutorials and have yet to find anything missing.
I built a new workspace from scratch (again). I can't think of anything I did differently this time than last, but I can no longer reproduce the issue. Should this question be closed or deleted?
Post your last comment as an answer, then tick the checkmark next to it.
As to your issue: this could've been an issue with CMake caching certain variables in
CMakeCache.txt
.Maybe so. I originally had the CMakeList lines in the wrong order. I had hoped
catkin_make clean
would have fixed that.