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

Undefined reference to ros::init on Jade [closed]

asked 2016-08-09 04:04:02 -0600

Lizhuo gravatar image

updated 2016-08-10 10:59:04 -0600

I'm a freshman on ROS. And recently I come up with a problem when I learn the Tutorials(WritingPublisherSubscriber(c++)).

When I add the talker.cpp to the project, It appear I have problem with Undefined reference

Here is my talker.cpp

#include "ros/ros.h"
#include "std_msgs/String.h"

#include <sstream>


int main(int argc, char **argv)
{

   ros::init(argc, argv, "talker");


   return 0;
}

The CMakeLists.txt file has following content

cmake_minimum_required(VERSION 2.8.3)
project(beginner_tutorials)

## Find catkin and any catkin packages
find_package(catkin REQUIRED COMPONENTS 
roscpp 
rospy 
std_msgs 
message_generation)

## Declare ROS messages and services
add_message_files(FILES Num.msg)
add_service_files(FILES AddTwoInts.srv)

## Generate added messages and services
generate_messages(DEPENDENCIES std_msgs)

## Declare a catkin package
catkin_package()

## Build talker and listener
include_directories(include ${catkin_INCLUDE_DIRS})

add_executable(talker src/talker.cpp)
target_link_libraries(talker ${catkin_LIBRARIES})
add_dependencies(talker beginner_tutorials_generate_messages_cpp)

and Package.xml is here

<?xml version="1.0"?>
<package>
 <name>beginner_tutorials</name>
   <version>0.0.0</version>
 <description>The beginner_tutorials package</description>


  <maintainer email="lizhuo@todo.todo">lizhuo</maintainer>
  <license>TODO</license>

  <buildtool_depend>catkin</buildtool_depend>

  <build_depend>roscpp</build_depend>
  <build_depend>rospy</build_depend>
  <build_depend>std_msgs</build_depend>
  <build_depend>message_generation</build_depend>
  <run_depend>message_runtime</run_depend>
  <run_depend>roscpp</run_depend>
  <run_depend>rospy</run_depend>
  <run_depend>std_msgs</run_depend>


</package>

While compiling the above code using catkin_make, the error is

Base path: /home/lizhuo/catkin_lz
Source space: /home/lizhuo/catkin_lz/src
Build space: /home/lizhuo/catkin_lz/build
Devel space: /home/lizhuo/catkin_lz/devel
Install space: /home/lizhuo/catkin_lz/install
####
#### Running command: "make cmake_check_build_system" in "/home/lizhuo/catkin_lz/build"
####
####
#### Running command: "make -j4 -l4" in "/home/lizhuo/catkin_lz/build"
####
[  0%] Built target std_msgs_generate_messages_py
[  0%] Built target std_msgs_generate_messages_cpp
[  0%] Built target std_msgs_generate_messages_lisp
[  0%] [  0%] Built target _beginner_tutorials_generate_messages_check_deps_Num
Built target std_msgs_generate_messages_eus
[  0%] Built target _beginner_tutorials_generate_messages_check_deps_AddTwoInts
[ 33%] Built target beginner_tutorials_generate_messages_py
[ 50%] [ 66%] Built target beginner_tutorials_generate_messages_cpp
Built target beginner_tutorials_generate_messages_lisp
[ 91%] Built target beginner_tutorials_generate_messages_eus
[ 91%] Linking CXX executable ../devel/lib/beginner_tutorials/talker
Built target beginner_tutorials_generate_messages
CMakeFiles/talker.dir/src/talker.cpp.o:in the function ‘main’:
talker.cpp:(.text+0x55):对‘ros::init(int&, char**, std::__cxx11::basic_string<char, std::char_traits<char>,       std::allocator<char> > const&, unsigned int)’未定义的引用 (it means undefined reference)
collect2: error:ld return 1
beginner_tutorials/CMakeFiles/talker.dir/build.make:102: recipe for target  'devel/lib/beginner_tutorials/talker' failed
make[2]: *** [devel/lib/beginner_tutorials/talker] Error 1
CMakeFiles/Makefile2:1091: recipe for target 'beginner_tutorials/CMakeFiles/talker.dir/all' failed
make[1]: *** [beginner_tutorials/CMakeFiles/talker.dir/all] Error 2
Makefile:117: recipe for target 'all' failed
make: *** [all] Error 2
Invoking "make -j4 -l4" failed

I look though many answers, but cannot solve my problem. So I really need help. Thanks!

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by ahendrix
close date 2016-08-10 22:55:27.054929

Comments

I don't know if it will work but you can try to write <ros/ros.h> instead of "ros/ros.h"

lfr gravatar image lfr  ( 2016-08-09 05:14:56 -0600 )edit

I have try it. But it doesn't work. @Ifr

Lizhuo gravatar image Lizhuo  ( 2016-08-09 10:55:08 -0600 )edit

Why do you have your add_dependencies after target_link_libraries?

alecive gravatar image alecive  ( 2016-08-10 14:32:53 -0600 )edit

Just follow the tutorial. The problem is discovered, that the version of GCC is changed from 4.9 to 5. When I switch the version back to 4.9, It works! @alecive

Lizhuo gravatar image Lizhuo  ( 2016-08-10 22:05:15 -0600 )edit

1 Answer

Sort by » oldest newest most voted
2

answered 2016-08-09 11:36:27 -0600

ahendrix gravatar image

From the reference to std::__cxx11::basic_string in the link error, it looks like your compiler is using the C++11 standard libraries, but most of ROS is usually compiled with the previous version of C++ ( C++98 ).

If you have an environment variable or some other setting that is putting the compiler into C++11 mode, you should disable that.

Usually, the default compiler flags are fine, but you haven't said which operating system you're using or how you installed ROS, so I can't be certain.

edit flag offensive delete link more

Comments

my operating system is Ubuntu 15.04 and my GCC version is 5. Could you tell me how to disable C++11? Thank you~@ahendrix

Lizhuo gravatar image Lizhuo  ( 2016-08-09 21:50:55 -0600 )edit

It looks like the default compiler for Ubuntu 15.04 is probably GCC 4.9.2. Did you switch to GCC 5?

ahendrix gravatar image ahendrix  ( 2016-08-09 23:31:48 -0600 )edit

Yes. I have switched. But I try to change GCC version back to 4.9, it also doesn't work. @ahendrix

Lizhuo gravatar image Lizhuo  ( 2016-08-10 05:09:32 -0600 )edit

"it also doesn't work" is not helpful. Were you not able to switch the compiler back to the default version, or does it still fail to compile with the default compiler? Does it give the same error message or a new error message?

ahendrix gravatar image ahendrix  ( 2016-08-10 13:03:42 -0600 )edit

If you successfully switched back to the default version of gcc, you probably still have the old object files that contain bad references. You probably need to clean your workspace and rebuild. Either run catkin_make clean or remove the auto-generated build and devel folders in your workspace.

ahendrix gravatar image ahendrix  ( 2016-08-10 13:06:27 -0600 )edit

Thanks! My problem has solved. First, I switch the GCC version back to 4.9. Then run CATKIN_MAKE CLEAN to clean the build file. And when I run CATKIN_MAKE this time, it is OK! Thank you @ahendrix !

Lizhuo gravatar image Lizhuo  ( 2016-08-10 22:01:08 -0600 )edit

should I close this problem?

Lizhuo gravatar image Lizhuo  ( 2016-08-10 22:01:31 -0600 )edit

I still get the undefined reference to ros::init after trying these fixes. I've written up my problem in a separate question here. Is there something simple I'm missing that one of you can point out?

tsbertalan gravatar image tsbertalan  ( 2017-02-11 09:54:24 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2016-08-09 04:04:02 -0600

Seen: 740 times

Last updated: Aug 10 '16