Rosanswers logo

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)

## Find catkin and any catkin packages
find_package(catkin REQUIRED COMPONENTS 

## 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

## 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"?>
 <description>The beginner_tutorials package</description>

  <maintainer email="[email protected]">lizhuo</maintainer>




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!

Originally posted by Lizhuo on ROS Answers with karma: 3 on 2016-08-09

Post score: 0

Original comments

Comment by lfr on 2016-08-09:
I don't know if it will work but you can try to write <ros/ros.h> instead of "ros/ros.h"

Comment by Lizhuo on 2016-08-09:
I have try it. But it doesn't work. @Ifr

Comment by alecive on 2016-08-10:
Why do you have your add_dependencies after target_link_libraries?

Comment by Lizhuo on 2016-08-10:
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


1 Answer 1


Rosanswers logo

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.

Originally posted by ahendrix with karma: 47576 on 2016-08-09

This answer was ACCEPTED on the original site

Post score: 2

Original comments

Comment by Lizhuo on 2016-08-09:
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

Comment by ahendrix on 2016-08-09:
It looks like the default compiler for Ubuntu 15.04 is probably GCC 4.9.2. Did you switch to GCC 5?

Comment by Lizhuo on 2016-08-10:
Yes. I have switched. But I try to change GCC version back to 4.9, it also doesn't work. @ahendrix

Comment by ahendrix on 2016-08-10:
"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?

Comment by ahendrix on 2016-08-10:
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.

Comment by Lizhuo on 2016-08-10:
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 !

Comment by Lizhuo on 2016-08-10:
should I close this problem?

Comment by tsbertalan on 2017-02-11:
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?

Comment by ahendrix on 2017-02-11:
Nope. I saw your other question a few weeks back and didn't have anything to add.


Your Answer

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