ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Undefined reference to ros::init

asked 2016-02-15 12:19:31 -0500

fantabulum gravatar image

updated 2016-02-16 20:48:07 -0500

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)

find_package(catkin REQUIRED COMPONENTS

message("catkin libraries" ${catkin_LIBRARIES})

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


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 toros::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/ /opt/ros/indigo/lib/ -lboost_date_time /opt/ros/indigo/lib/ -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 ...
edit retag flag offensive close merge delete


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 a message("catkin libraries " ${catkin_LIBRARIES}) to your CMakeLists.txt and inspect that (it is less messy than looking at the /usr/bin/c++ line.

lucasw gravatar image lucasw  ( 2016-02-15 14:02:51 -0500 )edit

I would also use #include <ros/ros.h> (note the <> instead of "") Edit: Nvm, that shouldn't matter.

spmaniato gravatar image spmaniato  ( 2016-02-15 14:07:55 -0500 )edit

@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.

fantabulum gravatar image fantabulum  ( 2016-02-16 20:50:04 -0500 )edit

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?

fantabulum gravatar image fantabulum  ( 2016-02-16 21:53:54 -0500 )edit

Post your last comment as an answer, then tick the checkmark next to it.

gvdhoorn gravatar image gvdhoorn  ( 2016-02-17 02:09:31 -0500 )edit

As to your issue: this could've been an issue with CMake caching certain variables in CMakeCache.txt.

gvdhoorn gravatar image gvdhoorn  ( 2016-02-17 02:10:25 -0500 )edit

Maybe so. I originally had the CMakeList lines in the wrong order. I had hoped catkin_make clean would have fixed that.

fantabulum gravatar image fantabulum  ( 2016-02-17 09:57:01 -0500 )edit

1 Answer

Sort by » oldest newest most voted

answered 2016-02-17 10:07:12 -0500

fantabulum gravatar image

updated 2016-02-17 15:20:18 -0500

Even though this was a new workspace, I created another one from scratch. In the workspace I was working in at the time, I originally had the CMakeList lines out of order. Running catkin_make clean didn't have an affect on my issue. After creating a new workspace, I copied the random_walk folder that is verbatim to what I have posted above and the problem magically vanished.

mkdir -p ~/test_space/src
cd ~/test_space/src/

cd ..
# /opt/ros/indigo/setup.bash already sourced in .bashrc
source devel/setup.bash
cp -r ~/other_wksp_path/src/random_walk ./src/
catkin_make random_walk
edit flag offensive delete link more



in catkin_ws rm -rf build devel install ought to work as a 'superclean' that maybe would have also solved your problem. The only thing left over that could mess up your build would maybe be an old or somehow corrupted catkin_ws/src/CMakeLists.txt, rm and catkin_init_workspace to make a new one.

lucasw gravatar image lucasw  ( 2016-03-23 10:39:56 -0500 )edit

Question Tools



Asked: 2016-02-15 12:19:31 -0500

Seen: 5,955 times

Last updated: Feb 17 '16