Ask Your Question

How to solve 'undefined reference to `ros::init' on Groovy?

asked 2013-05-27 19:29:47 -0600

sam gravatar image

updated 2013-05-27 19:36:55 -0600

I use ROS groovy on ubuntu 12.04 64 bits.

I try to write the simplest program.

I run:

  cd ../src
  catkin_create_pkg sam_hello roscpp
  roscd sam_hello


  #include <ros/ros.h>

  int main(int argc,char **argv)
      ros::init(argc,argv, "sam_hello_node");
      return 0;

Then I edit CMakeLists.txt,add a line:

  add_executable(sam_hello_node src/sam_hello_src1.cpp)

Then I try to make:

  cd ..

It shows:

  sam@sam:~/code/ros_groovy$ catkin_make
  Base path: /home/sam/code/ros_groovy
  Source space: /home/sam/code/ros_groovy/src
  Build space: /home/sam/code/ros_groovy/build
  Devel space: /home/sam/code/ros_groovy/devel
  Install space: /home/sam/code/ros_groovy/install
  #### Running command: "make cmake_check_build_system" in "/home/sam/code/ros_groovy/build"
  #### Running command: "make -j4 -l4" in "/home/sam/code/ros_groovy/build"
  Scanning dependencies of target sam_hello_node
  [100%] Building CXX object sam_hello/CMakeFiles/sam_hello_node.dir/src/sam_hello_src1.cpp.o
  Linking CXX executable /home/sam/code/ros_groovy/devel/lib/sam_hello/sam_hello_node
  CMakeFiles/sam_hello_node.dir/src/sam_hello_src1.cpp.o: In function `main':
  sam_hello_src1.cpp:(.text+0x46): undefined reference to `ros::init(int&, char**, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int)'
  sam_hello_src1.cpp:(.text+0x63): undefined reference to `ros::spin()'
  collect2: ld 回傳 1
  make[2]: *** [/home/sam/code/ros_groovy/devel/lib/sam_hello/sam_hello_node] Error 1
  make[1]: *** [sam_hello/CMakeFiles/sam_hello_node.dir/all] Error 2
  make: *** [all] Error 2
  Invoking "make" failed

How to solve it?

Thank you~

---The following is the process that I setup groovy environments---


  source /opt/ros/groovy/
  mkdir -p ~/code/ros_groovy/src
  cd ~/code/ros_groovy/src
  cd ~/code/ros_groovy/
  echo 'source $HOME/code/ros_groovy/devel/setup.bash' >> ~/.bashrc
  source devel/setup.bash

Setting process:

  sam@sam:~$ source /opt/ros/groovy/
  sam@sam:~$ mkdir -p ~/code/ros_groovy/src
  sam@sam:~$ cd ~/code/ros_groovy/src
  sam@sam:~/code/ros_groovy/src$ catkin_init_workspace
  Creating symlink "/home/sam/code/ros_groovy/src/CMakeLists.txt" pointing to "/opt/ros/groovy/share/catkin/cmake/toplevel.cmake"
  sam@sam:~/code/ros_groovy/src$ cd ~/code/ros_groovy/
  sam@sam:~/code/ros_groovy$ catkin_make
  Base path: /home/sam/code/ros_groovy
  Source space: /home/sam/code/ros_groovy/src
  Build space: /home/sam/code/ros_groovy/build
  Devel space: /home/sam/code/ros_groovy/devel
  Install space: /home/sam/code/ros_groovy/install
  #### Running command: "cmake /home/sam/code/ros_groovy/src -DCATKIN_DEVEL_PREFIX=/home/sam/code/ros_groovy/devel -DCMAKE_INSTALL_PREFIX=/home/sam/code/ros_groovy/install" in "/home/sam/code/ros_groovy/build"
  -- The C compiler identification is GNU
  -- The CXX compiler identification is GNU
  -- Check for working C compiler: /usr/bin/gcc
  -- Check for working C compiler: /usr/bin/gcc -- works
  -- Detecting C compiler ABI info
  -- Detecting C compiler ABI info - done
  -- Check for working CXX compiler: /usr/bin/c++
  -- Check for working CXX compiler: /usr/bin/c++ -- works
  -- Detecting CXX compiler ABI info
  -- Detecting CXX compiler ABI info - done
  -- Using CATKIN_DEVEL_PREFIX: /home/sam/code/ros_groovy/devel
  -- Using CMAKE_PREFIX_PATH: /opt/ros/groovy
  -- This workspace overlays: /opt/ros/groovy
  -- Found PythonInterp: /usr/bin/python ...
edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2013-05-27 22:29:25 -0600

fivef gravatar image



to your CMakeLists.txt

edit flag offensive delete link more



Here is the current draft of some catkin tutorials I am working on:

joq gravatar imagejoq ( 2013-05-28 05:17:07 -0600 )edit

@fivef right now im working with pcl17 and with the program to detect people. when I included ros/ros.h and then ros::init(argc, argv, "talker") in the code and tried to compile; i got the following error "undefined reference to `ros::init", do you now why and how to solve it?? I would really appreciate some help

ctguell gravatar imagectguell ( 2013-10-10 06:40:37 -0600 )edit

That's because you are not linking against `roscpp`. Things to check, (assuming `catkin`) do you build_depend on `roscpp` in your `package.xml`? do you `find_package(catkin REQUIRED COMPONENTS roscpp <other_things>)`? do you `target_link_libraries(<your_program_or_library_name> ${catkin_LIBRARIES})`?

William gravatar imageWilliam ( 2013-10-10 06:59:20 -0600 )edit

@William im using rosbiuld do you have any idea how to solve it in rosbuild?

ctguell gravatar imagectguell ( 2013-10-10 07:34:47 -0600 )edit

@ctguell Make sure you depend (`<depend package="roscpp"/>`) on `roscpp` in your `manifest.xml` and that you are using `rosbuild_add_executable` or `rosbuild_add_library` for your executables or libraries respectively.

William gravatar imageWilliam ( 2013-10-10 07:48:52 -0600 )edit

@William i added <depend package="roscpp"/> to the maifest.xml, where should i look to see if im using `rosbuild_add_executable` or `rosbuild_add_library`?

ctguell gravatar imagectguell ( 2013-10-10 08:21:08 -0600 )edit

@ctguell in your `CMakeLists.txt`

William gravatar imageWilliam ( 2013-10-10 08:28:10 -0600 )edit

@William right now i have this PCL_ADD_EXECUTABLE(pcl_ground_based_rgbd_people_detector ${SUBSYS_NAME} apps/main_ground_based_people_detection.cpp) target_link_libraries(pcl_ground_based_rgbd_people_detector pcl_people pcl_common pcl_kdtree pcl_search pcl_features pcl_sample_consensus pcl_filters pcl_io pcl_visualization pcl_segmentation) , how should i add the ros library??

ctguell gravatar imagectguell ( 2013-10-10 08:37:05 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2013-05-27 19:29:47 -0600

Seen: 18,474 times

Last updated: May 27 '13