Rosanswers logo

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 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'
make[2]: *** [random_walk/CMakeFiles/random_walk.dir/all] Error 2
make[2]: Leaving directory `/home/gary/catkin_ws/build'
make[1]: *** [random_walk/CMakeFiles/random_walk.dir/rule] Error 2
make[1]: Leaving directory `/home/gary/catkin_ws/build'
make: *** [random_walk] Error 2
Base path: /home/gary/catkin_ws
Source space: /home/gary/catkin_ws/src
Build space: /home/gary/catkin_ws/build
Devel space: /home/gary/catkin_ws/devel
Install space: /home/gary/catkin_ws/install
#### Running command: "make cmake_check_build_system" in "/home/gary/catkin_ws/build"
#### Running command: "make -j1 random_walk VERBOSE=1" in "/home/gary/catkin_ws/build"
Invoking "make -j1 random_walk VERBOSE=1" failed

Originally posted by fantabulum on ROS Answers with karma: 21 on 2016-02-15

Post score: 2

Original comments

Comment by lucasw on 2016-02-15:
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.

Comment by spmaniato on 2016-02-15:
I would also use #include <ros/ros.h> (note the <> instead of "") Edit: Nvm, that shouldn't matter.

Comment by fantabulum on 2016-02-16:
@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.

Comment by fantabulum on 2016-02-16:
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?

Comment by gvdhoorn on 2016-02-17:
Post your last comment as an answer, then tick the checkmark next to it.

Comment by gvdhoorn on 2016-02-17:
As to your issue: this could've been an issue with CMake caching certain variables in CMakeCache.txt.

Comment by fantabulum on 2016-02-17:
Maybe so. I originally had the CMakeList lines in the wrong order. I had hoped catkin_make clean would have fixed that.


1 Answer 1


Rosanswers logo

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

Originally posted by fantabulum with karma: 21 on 2016-02-17

This answer was ACCEPTED on the original site

Post score: 0

Original comments

Comment by lucasw on 2016-03-23:
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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.